A comparator interface is used to order the objects of user-defined classes. A comparator object is capable of comparing two objects of two different classes. Following function compare obj1 with obj2
Syntax:
public int compare(Object obj1, Object obj2):
Suppose we have an Array/ArrayList of our own class type, containing fields like roll no, name, address, DOB, etc, and we need to sort the array based on Roll no or name?
Method 1: One obvious approach is to write our own sort() function using one of the standard algorithms. This solution requires rewriting the whole sorting code for different criteria like Roll No. and Name.
Method 2: Using comparator interface- Comparator interface is used to order the objects of a user-defined class. This interface is present in java.util package and contains 2 methods compare(Object obj1, Object obj2) and equals(Object element). Using a comparator, we can sort the elements based on data members. For instance, it may be on roll no, name, age, or anything else.
Method of Collections class for sorting List elements is used to sort the elements of List by the given comparator.
// To sort a given list. ComparatorClass must implement
// Comparator interface.
public void sort(List list, ComparatorClass c)
How does Collections.Sort() work?
Internally the Sort method does call Compare method of the classes it is sorting. To compare two elements, it asks “Which is greater?” Compare method returns -1, 0, or 1 to say if it is less than, equal, or greater to the other. It uses this result to then determine if they should be swapped for their sort.
Working Program:
// Java program to demonstrate working of Comparator
// interface
import java.io.*;
import java.lang.*;
import java.util.*;
// A class to represent a student.
class Student {
int roll_no;
String name, address;
// Constructor
public Student(int roll_no, String name, String address)
{
this.roll_no = roll_no;
this.name = name;
this.address = address;
}
// Used to print student details in main()
public String toString()
{
return this.roll_no + " " + this.name + " "
+ this.address;
}
}
class SortbyRoll implements Comparator<Student> {
// Used for sorting in ascending order of
// roll number
public int compare(Student a, Student b)
{
return a.roll_no - b.roll_no;
}
}
class SortbyName implements Comparator<Student> {
// Used for sorting in ascending order of
// name
public int compare(Student a, Student b)
{
return a.name.compareTo(b.name);
}
}
// Driver class
class Main {
public static void main(String[] args)
{
ArrayList<Student> ar = new ArrayList<Student>();
ar.add(new Student(222, "bbbb", "london"));
ar.add(new Student(242, "aaaa", "nyc"));
ar.add(new Student(121, "cccc", "jaipur"));
System.out.println("Unsorted");
for (int i = 0; i < ar.size(); i++)
System.out.println(ar.get(i));
Collections.sort(ar, new SortbyRoll());
System.out.println("nSorted by roll_no");
for (int i = 0; i < ar.size(); i++)
System.out.println(ar.get(i));
Collections.sort(ar, new SortbyName());
System.out.println("nSorted by name");
for (int i = 0; i < ar.size(); i++)
System.out.println(ar.get(i));
}
}
Output
Unsorted
222 bbbb london
242 aaaa nyc
232 cccc jaipur
Sorted by roll_no
222 bbbb london
232 cccc jaipur
242 aaaa nyc
Sorted by name
242 aaaa nyc
222 bbbb london
232 cccc jaipur
By changing the return value inside the compare method, you can sort in any order that you wish to. Eg. for descending order just change the positions of ‘a’ and ‘b’ in the above compare method.
Sort collection by more than one field:
In previous articles, we have discussed how to sort the list of objects on the basis of a single field using Comparable and Comparator interface But, what if we have a requirement to sort ArrayList objects in accordance with more than one fields like firstly, sort according to the student name and secondly, sort according to student age.
Below is the implementation of the above approach:
// Java program to demonstrate working of Comparator
// interface more than one field
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
class Student {
// instance member variables
String Name;
int Age;
// parameterized constructor
public Student(String Name, Integer Age)
{
this.Name = Name;
this.Age = Age;
}
public String getName() { return Name; }
public void setName(String Name) { this.Name = Name; }
public Integer getAge() { return Age; }
public void setAge(Integer Age) { this.Age = Age; }
// overriding toString() method
@Override public String toString()
{
return "Customer{"
+ "Name=" + Name + ", Age=" + Age + '}';
}
static class CustomerSortingComparator
implements Comparator<Student> {
@Override
public int compare(Student customer1,
Student customer2)
{
// for comparison
int NameCompare = customer1.getName().compareTo(
customer2.getName());
int AgeCompare = customer1.getAge().compareTo(
customer2.getAge());
// 2-level comparison
return (NameCompare == 0) ? AgeCompare
: NameCompare;
}
}
public static void main(String[] args)
{
// create ArrayList to store Student
List<Student> al = new ArrayList<>();
// create customer objects using constructor
// initialization
Student obj1 = new Student("suresh", 27);
Student obj2 = new Student("vibha", 25);
Student obj3 = new Student("Sarika", 37);
Student obj4 = new Student("suresh", 32);
Student obj5 = new Student("suresh", 29);
Student obj6 = new Student("vibha", 32);
// add customer objects to ArrayList
al.add(obj1);
al.add(obj2);
al.add(obj3);
al.add(obj4);
al.add(obj5);
al.add(obj6);
// before Sorting arraylist: iterate using Iterator
Iterator<Student> custIterator = al.iterator();
System.out.println("Before Sorting:n");
while (custIterator.hasNext()) {
System.out.println(custIterator.next());
}
// sorting using Collections.sort(al, comparator);
Collections.sort(al,
new CustomerSortingComparator());
// after Sorting arraylist: iterate using enhanced
// for-loop
System.out.println("nnAfter Sorting:n");
for (Student customer : al) {
System.out.println(customer);
}
}
}
Output
Before Sorting:
Customer{Name=suresh, Age=27}
Customer{Name=vibha, Age=25}
Customer{Name=Sarika, Age=37}
Customer{Name=suresh, Age=32}
Customer{Name=suresh, Age=29}
Customer{Name=vibha, Age=32}
After Sorting:
Customer{Name=suresh, Age=32}
Customer{Name=suresh, Age=27}
Customer{Name=suresh, Age=29}
Customer{Name=Sarika, Age=37}
Customer{Name=vibha, Age=32}
Customer{Name=vibha, Age=25}