Sorting Object By one or More Properties in Java


While working with arrays or lists or any other collection in java, many times we need to sort elements. For primitives and strings, it can be straightforward. But for customer objects, we need to write Comparator or implement a Comparable interface.

If you are dealing with objects without Comparable interface or you need to perform sorting on another field(s) then you need to use a Comparator.

For example, we have the following employee class

class Employee {
    private String name;
    private int age;
    private double salary;

    public Employee(String name, int age, double salary) {
        this.name = name;
        this.age = age;
        this.salary = salary;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public double getSalary() {
        return salary;
    }
    public void setSalary(double salary) {
        this.salary = salary;
    }
}

Sorting Objects using Comparator Interface

We want to sort ArrayList of Employee object based on names, then we need to write one comparator class as given below.

class EmployeeComparator implements Comparator<Employee> {
    @Override
    public int compare(Employee o1, Employee o2) {
        return o1.getName().compareTo(o2.getName());
    }
}

and call Collections.sort method

Collections.sort(empList, new EmployeeComparator());
for (Employee e : empList) {
    System.out.println(e.getName());
}

If you want to sort based on Employee class name then salary property then you need to implement Comparator as given below.

class EmployeeComparator implements Comparator<Employee> {
    @Override
    public int compare(Employee o1, Employee o2) {
       int t= o1.getName().compareTo(o2.getName());
       if(t==0){
           t = Double.compare(o1.getSalary(), o2.getSalary());
       }
       return t;
    }
}

In the above comparator code first compared to the name and if the comparison result is zero, means names are the same then comparing salary.

Writing Comerator classes are making code look clean but you can also write anonymous class for the comparator interface.

Collections.sort(empList, new Comparator<Employee>() {
    @Override
    public int compare(Employee o1, Employee o2) {
        int t= o1.getName().compareTo(o2.getName());
        if(t==0){
            t = Double.compare(o1.getSalary(), o2.getSalary());
        }
        return t;
    }
});

Sorting Object using Lambda Expression

Since Java 8, you can sort java objects based on their properties using lambda expressions.

Collections.sort(empList, (Employee e1, Employee e2) ->{
    return e1.getName().compareToIgnoreCase(e2.getName());
});

Or

Comparator<Employee> c = (e1, e2) -> e1.getName().compareTo(e2.getName());
empList.sort(c);

For multiple property comparator using lambda write as given below

Comparator<Employee> c = (e1, e2) -> {
    int res = e1.getName().compareTo(e2.getName());
    if(res==0){
        res = Double.compare(e1.getSalary(), e2.getSalary());
    }
    return res;
};
empList.sort(c);

above code will first compare name, if names are equal then it will compare salary.

for sorting based on 2 or more properties of an object just implement a comparator as given above.


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.