Java Sort Array Objects using Comparator Interface

In this tutorial, we will learn how to sort an array of a custom object (Employee) with a Comparator Interface.
Also, check out Java Sort Array Objects using Comparable Interface.
Check out Comparator example at https://www.javaguides.net/2018/12/java-comparator-interface-example.html
Learn Java Array at https://www.javaguides.net/2020/04/java-array-tutorial-for-beginners.html.

Sort an Array of Employee objects (by salary and name) in Ascending Order

Let's first create an Employee class:
package com.java.array.tutorial.sorting;

public class Employee implements Comparable < Employee > {
    private int id;
    private String name;
    private int salary;


    public Employee(int id, String name, int salary) {
        super();
        this.id = id;
        this.name = name;
        this.salary = salary;
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getSalary() {
        return salary;
    }
    public void setSalary(int salary) {
        this.salary = salary;
    }
    @Override
    public String toString() {
        return "Employee [id=" + id + ", name=" + name + ", salary=" + salary + "]";
    }
    @Override
    public int compareTo(Employee o) {
        return this.salary - o.salary;
    }
}
package com.java.array.tutorial.sorting;

import java.util.Arrays;
import java.util.Comparator;

public class SortArray {
    public static void main(String[] args) {

        // sort Array of employee objects using Comparator interface

        Employee[] employees = {
            new Employee(10, "Ramesh", 50000),
            new Employee(20, "John", 30000),
            new Employee(30, "tom", 40000),
            new Employee(40, "Tony", 60000)
        };

        System.out.println(" Before sorting => " + Arrays.toString(employees));
        Arrays.sort(employees, new SortEmployeeBySalary());
        System.out.println(" After sorting Employee by salary => " + Arrays.toString(employees));

        Arrays.sort(employees, new SortEmployeeByName());
        System.out.println(" After sorting Employee by name=> " + Arrays.toString(employees));

    }
}

class SortEmployeeBySalary implements Comparator < Employee > {
    @Override
    public int compare(Employee o1, Employee o2) {
        return o1.getSalary() - o2.getSalary();
    }
}


class SortEmployeeByName implements Comparator < Employee > {

    @Override
    public int compare(Employee o1, Employee o2) {
        return o1.getName().compareTo(o2.getName());
    }
}
Output:
 Before sorting => [Employee [id=10, name=Ramesh, salary=50000], Employee [id=20, name=John, salary=30000], Employee [id=30, name=tom, salary=40000], Employee [id=40, name=Tony, salary=60000]]
 After sorting Employee by salary => [Employee [id=20, name=John, salary=30000], Employee [id=30, name=tom, salary=40000], Employee [id=10, name=Ramesh, salary=50000], Employee [id=40, name=Tony, salary=60000]]
 After sorting Employee by name=> [Employee [id=20, name=John, salary=30000], Employee [id=10, name=Ramesh, salary=50000], Employee [id=40, name=Tony, salary=60000], Employee [id=30, name=tom, salary=40000]]

Sort an Array of Employee objects (by salary and name) in Descending Order

package com.java.array.tutorial.sorting;

import java.util.Arrays;
import java.util.Comparator;

public class SortArray {
    public static void main(String[] args) {

        // sort Array of employee objects using Comparator interface

        Employee[] employees = {
            new Employee(10, "Ramesh", 50000),
            new Employee(20, "John", 30000),
            new Employee(30, "tom", 40000),
            new Employee(40, "Tony", 60000)
        };

        System.out.println(" Before sorting => " + Arrays.toString(employees));
        Arrays.sort(employees, new SortEmployeeBySalary());
        System.out.println(" SortEmployeeBySalary=> " + Arrays.toString(employees));
        Arrays.sort(employees, new SortEmployeeByName());
        System.out.println(" SortEmployeeByName => " + Arrays.toString(employees));


    }
}

class SortEmployeeBySalary implements Comparator < Employee > {

    @Override
    public int compare(Employee o1, Employee o2) {
        return o2.getSalary() - o1.getSalary();
    }

}


class SortEmployeeByName implements Comparator < Employee > {

    @Override
    public int compare(Employee o1, Employee o2) {
        return o2.getName().compareTo(o1.getName());
    }
}
Output:
 Before sorting => [Employee [id=10, name=Ramesh, salary=50000], Employee [id=20, name=John, salary=30000], Employee [id=30, name=tom, salary=40000], Employee [id=40, name=Tony, salary=60000]]
 SortEmployeeBySalary=> [Employee [id=40, name=Tony, salary=60000], Employee [id=10, name=Ramesh, salary=50000], Employee [id=30, name=tom, salary=40000], Employee [id=20, name=John, salary=30000]]
 SortEmployeeByName => [Employee [id=30, name=tom, salary=40000], Employee [id=40, name=Tony, salary=60000], Employee [id=10, name=Ramesh, salary=50000], Employee [id=20, name=John, salary=30000]]

Java 8 Lambda - Sort an Array of Employee objects (by salary) in Ascending Order

package com.java.array.tutorial.sorting;

import java.util.Arrays;

public class SortArray {
    public static void main(String[] args) {

        // sort Array of employee objects using Comparator interface

        Employee[] employees = {
            new Employee(10, "Ramesh", 50000),
            new Employee(20, "John", 30000),
            new Employee(30, "tom", 40000),
            new Employee(40, "Tony", 60000)
        };

        System.out.println(" Before sorting => " + Arrays.toString(employees));
        Arrays.sort(employees, (o1, o2) - > o1.getSalary() - o2.getSalary());
        System.out.println(" After sorting => " + Arrays.toString(employees));
    }
}
Output:
 Before sorting => [Employee [id=10, name=Ramesh, salary=50000], Employee [id=20, name=John, salary=30000], Employee [id=30, name=tom, salary=40000], Employee [id=40, name=Tony, salary=60000]]
 After sorting => [Employee [id=20, name=John, salary=30000], Employee [id=30, name=tom, salary=40000], Employee [id=10, name=Ramesh, salary=50000], Employee [id=40, name=Tony, salary=60000]]

Java 8 Lambda - Sort an Array of Employee objects (by salary) in Descending Order

package com.java.array.tutorial.sorting;

import java.util.Arrays;

public class SortArray {
    public static void main(String[] args) {

        // sort Array of employee objects using Comparator interface

        Employee[] employees = {
            new Employee(10, "Ramesh", 50000),
            new Employee(20, "John", 30000),
            new Employee(30, "tom", 40000),
            new Employee(40, "Tony", 60000)
        };

        System.out.println(" Before sorting => " + Arrays.toString(employees));
        Arrays.sort(employees, (o1, o2) - > o2.getSalary() - o1.getSalary());
        System.out.println(" After sorting => " + Arrays.toString(employees));
    }
}
Output:
 Before sorting => [Employee [id=10, name=Ramesh, salary=50000], Employee [id=20, name=John, salary=30000], Employee [id=30, name=tom, salary=40000], Employee [id=40, name=Tony, salary=60000]]
 After sorting => [Employee [id=40, name=Tony, salary=60000], Employee [id=10, name=Ramesh, salary=50000], Employee [id=30, name=tom, salary=40000], Employee [id=20, name=John, salary=30000]]



Free Spring Boot Tutorial | Full In-depth Course | Learn Spring Boot in 10 Hours


Watch this course on YouTube at Spring Boot Tutorial | Fee 10 Hours Full Course

Comments