Java sort objects using Comparator example shows how to sort custom objects using a Comparator by the object properties in ascending or descending order.
How to sort objects using a Comparator in Java?
In order to sort custom objects by its properties, we need to implement the compare
method of the Comparator interface.
1 |
public interface Comparator<T> |
Comparator interface has the compare
method which must be implemented by the class implementing the Comparator interface.
1 |
int compare(T object1, T object2) |
To sort objects in ascending order, this method should return 0 if the two objects are equal, a positive integer if the object1 is greater than object2 and a negative integer if object1 is less than object2.
For this example, we are going to sort ArrayList of Employee objects. We will sort the employee according to their age in ascending order.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
package com.javacodeexamples.basic.arrayexamples; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import com.javacodeexamples.common.Employee; public class SortObjectsUsingComparatorExample { public static void main(String[] args) { //arraylist of Employee objects ArrayList<Employee> aListEmployee = new ArrayList<Employee>(); //add employee objects to ArrayList aListEmployee.add( new Employee("1", "Mark", "Marketing", 45) ); aListEmployee.add( new Employee("2", "Lucy", "Customer Care", 24) ); aListEmployee.add( new Employee("3", "Brinda", "Public Relations", 37) ); aListEmployee.add( new Employee("4", "Raj", "IT", 22) ); /* * To sort an ArrayList of custom objects, use sort method of Collections class * along with the custom Comparator ojbect we created. */ Collections.sort(aListEmployee, new EmployeeComparator()); System.out.println(aListEmployee); } } class EmployeeComparator implements Comparator<Employee>{ /* * Compare method should return zero for equal values, positive integer if * value 1 is greater than value 2 and negative value if value 2 is less than * value 2 to sort objects in ascending order. */ public int compare(Employee emp1, Employee emp2){ //compare age of employees //if emp1 age is greater than emp2 age, return 1 if( emp1.getAge() > emp2.getAge() ){ return 1; //if emp1 age is less than emp2 age, return -1 }else if( emp1.getAge() < emp2.getAge() ){ return -1; //if equal }else{ return 0; } } } |
Output
1 |
[[4 : Raj : IT], [2 : Lucy : Customer Care], [3 : Brinda : Public Relations], [1 : Mark : Marketing]] |
How to sort objects in descending order using Comparator in Java?
In the above example we returned zero for equal age, a positive value if the age of employee1 is greater than employee2 and a negative value if the age of employee1 is less than the age of employee2. If you want to sort the Employee objects in descending order, you need to inverse the return values.
That means you need to return a negative value if employee1’s age is greater than employee2’s age and a positive integer if employee1’s age is less than employee2’s age property.
The compare
method needs to be changed as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
//to sort objects in descending order public int compare(Employee emp1, Employee emp2){ //compare age of employees //if emp1 age is greater than emp2 age, return -1 if( emp1.getAge() > emp2.getAge() ){ return -1; //if emp1 age is less than emp2 age, return 1 }else if( emp1.getAge() < emp2.getAge() ){ return 1; //if equal }else{ return 0; } } |
Output
1 |
[[1 : Mark : Marketing], [3 : Brinda : Public Relations], [2 : Lucy : Customer Care], [4 : Raj : IT]] |
This example is a part of the Java Comparator tutorial with examples. Please also check out the ArrayList in Java tutorial to know more.
Please let me know your views in the comments section below.