Skip to main content

How to sort objects using Comparator Interface

Points To Remember
  • Comparator is an interface and is available in java.util package.
  • A class that implement Comparator interface must override its compare() method. 
  • We use Comparator when we have to write logic for comparing object of a class outside that class.
  • compare(Object e1, Object e2): Can be called like compare(e1, e2)
    1. compare(Objecte obj1, Object obj2) method returns zero if the objects are equal.
    2. compare(Objecte obj1, Object obj2) method returns a positive value if obj1 is greater than obj2.
    3. compare(Objecte obj1, Object obj2) method returns a negative value if obj1 is smaller than obj2.
    4. It should throw ClassCastException if object types of e1 and e2 are not comparable.
    5. It should throw NullPointerException if either e1 or e2 or both passed are null.
Syntax for using Comparator interface.
First we need a class for which we need to write the sorting logic(how to compare objects of this class). Then we need to create a new class that will do this comparing for this class. And finally we need to  call the class to do sorting for us.
class TobeCompared{

// Variables
}

class ComparingLogic implements Comparator{

@Override
public int compare(Object obj1, Object obj2){

// Comparing logic
//return int value based on comparison
}
}

class Test{

public static void main(String args[]){

// Create List of TobeCompared
// call Collections.sort(list object, new ComparingLogic()) to sort the list

}
}

Example: Sort an Arraylist using Comparator Interface
Suppose we have a Person class with values name, age and gender. Now if we want to sort a list of Person class such that we get a list of Person class sorted by their name, if names are equal then by age and if age is also same then by their gender.

import java.util.*;
import java.util.Comparator;

class Person{

String name;
int age;
String gender;

public Person(String name, int age, String gender){

this.name=name;this.age=age;this.gender=gender;
}
}


public class SortObjects implements Comparator<Person>{

public static void main(String args[]){

List<Person> list = new ArrayList<Person>();
Person person;
person = new Person("Akash" , 23 , "Male");
list.add(person);
person = new Person("Sarah" , 18 , "Female");
list.add(person);
person = new Person("Garvita" , 52 , "Female");
list.add(person);
person = new Person("Sanket" , 38 , "Male");
list.add(person);
person = new Person("Sanket" , 38 , "Female");
list.add(person);

Collections.sort(list, new SortObjects());

for(Person obj : list){
System.out.println("name="+obj.name+" age="+obj.age+" gender="+obj.gender);
}

}

@Override
public int compare(Person p1, Person p2){

int result = 0;

result = p1.name.compareTo(p2.name);
if(result!=0)
return result;

result = p1.age - p2.age;
if(result!=0)
return result;

result = p1.gender.compareTo(p2.gender);
if(result!=0)
return result;

return result;
}

}
Output of the program :
name=Akash    age=23   gender=Male
name=Garvita  age=52   gender=Female
name=Sanket   age=38   gender=Female
name=Sanket   age=38   gender=Male
name=Sarah    age=18   gender=Female

We can even move the sorting logic from the class to be sorted to a new class, say ComparingLogic, where we can keep write comparing logic for more than one class by overloading the compare() method. So, this class can be used to sort any collection object in the entire project.

Comments