Java Comparator Interface
The Comparator interface in Java is used to sort the objects of user-defined classes. The Comparator interface is present in java.util package. This interface allows us to define custom comparison logic outside of the class for which instances we want to sort. The comparator interface is useful when,
- We need multiple sorting strategies for a class.
- When we want to keep the sorting logic separate from the class.
A comparator object is capable of comparing two objects of the same class. The following function compares obj1 with obj2.
Syntax:
public int compare(Object obj1, Object obj2):
- It will return a negative integer if obj1 < obj2.
- It will return 0 if both objects are equal.
- It will return a positive integer if obj1 > obj2.
When to Use Comparator?
Suppose we have a list of Student objects, containing fields like roll no, name, address, DOB, etc, and we need to sort them based on roll no or name. We use Comparator here to define that logic separately.
Methods to Implement Comparator Interface
- Method 1: One obvious approach is to write our 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: The Comparator interface is used to order the objects of a user-defined class. This interface contains 2 methods that are, 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.
How does the sort() Method of the Collections Class Work?
The sort() method of the Collections class is used to sort the elements of a List by the given comparator.
public void sort(List list, ComparatorClass c)
To sort a given List, ComparatorClass must implement a Comparator interface.
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.
Sort Collections by One Field
Example: Sorting By Roll Number
// Using Comparator Interface
import java.util.*;
// Define the Student class
class Student {
int rollno;
String name;
// Constructor
Student(int rollno, String name) {
this.rollno = rollno;
this.name = name;
}
// Method to print Student
// details in main()
@Override
public String toString() {
return rollno + ": " + name;
}
}
// Helper class implementing Comparator interface
class SortbyRoll implements Comparator<Student>
{
// Compare by roll number in ascending order
public int compare(Student a, Student b) {
return a.rollno - b.rollno;
}
}
// Driver Class
public class Geeks
{
public static void main(String[] args)
{
// List of Students
List<Student> students = new ArrayList<>();
// Add Elements in List
students.add(new Student(111, "Mayank"));
students.add(new Student(131, "Anshul"));
students.add(new Student(121, "Solanki"));
students.add(new Student(101, "Aggarwal"));
// Sort students by roll number
// using SortbyRoll comparator
Collections.sort(students, new SortbyRoll());
System.out.println("Sorted by Roll Number ");
// Iterating over entries to print them
for (int i = 0; i < students.size(); i++)
System.out.println(students.get(i));
}
}
Output
Sorted by Roll Number 101: Aggarwal 111: Mayank 121: Solanki 131: Anshul
By changing the return value inside the compare method, you can sort in any order that you wish to. For example: For descending order just change the positions of “a” and “b” in the above compare method.
Note: We can use lambda expression in place of helper function by following the statement as mentioned below:
students.sort((p1, p2) -> Integer.compare(p1.age, p2.age));
Sort Collection by More than One Field
In the previous example, we have discussed how to sort the list of objects on the basis of a single field using the Comparable and Comparator interface But, what if we have a requirement to sort ArrayList objects in accordance with more than one field like firstly, sort according to the student name and secondly, sort according to student age.
Example: Sorting By Multiple Fields (Name, then Age)
// Using Comparator Interface Via
// More than One Field
import java.util.*;
// Define the Student class
class Student
{
String name;
Integer age;
// Constructor
Student(String name, Integer age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public Integer getAge() {
return age;
}
// Method to print student
// details in main()
@Override
public String toString() {
return name + " : " + age;
}
}
// Comparator in a Helper Class
class CustomerSortingComparator
implements Comparator<Student>
{
// Compare first by name, then by age
public int compare(Student customer1, Student customer2) {
// Compare by name first
int NameCompare = customer1.getName().compareTo(
customer2.getName());
// If names are the same, compare by age
int AgeCompare = customer1.getAge().compareTo(
customer2.getAge());
// Return the result: first by name, second by age
return (NameCompare == 0) ? AgeCompare : NameCompare;
}
}
public class ComparatorHelperClassExample
{
public static void main(String[] args)
{
List<Student> students = new ArrayList<>();
students.add(new Student("Ajay", 27));
students.add(new Student("Sneha", 23));
students.add(new Student("Simran", 37));
students.add(new Student("Ankit", 22));
students.add(new Student("Anshul", 29));
students.add(new Student("Sneha", 22));
// Original List
System.out.println("Original List ");
// Iterating List
for (Student it : students) {
System.out.println(it);
}
System.out.println();
// Sort students by name, then by age
// using the CustomerSortingComparator
Collections.sort(students, new CustomerSortingComparator());
// Display message only
System.out.println("After Sorting ");
// Iterating using enhanced for-loop
// after Sorting ArrayList
for (Student it : students) {
System.out.println(it);
}
}
}
Output
Original List Ajay : 27 Sneha : 23 Simran : 37 Ankit : 22 Anshul : 29 Sneha : 22 After Sorting Ajay : 27 Ankit : 22 Anshul : 29 Simran : 37 Sneha : 22 Sneha : 23
Alternative Method: Using Comparator with Lambda
Java 8 introduced a more simple way to write comparators using lambda expressions. We can use the method mentioned below for achieving same result:
students.sort(Comparator.comparing(Student::getName).thenComparing(Student::getAge));
Example:
// Alternative Method
import java.util.*;
// Define the Student class
class Student {
String name;
Integer age;
// Constructor
Student(String name, Integer age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public Integer getAge() {
return age;
}
// Method to print student details
@Override
public String toString() {
return name + " : " + age;
}
}
public class ComparatorHelperClassExample {
public static void main(String[] args) {
List<Student> students = new ArrayList<>();
students.add(new Student("Ajay", 27));
students.add(new Student("Sneha", 23));
students.add(new Student("Simran", 37));
students.add(new Student("Ankit", 22));
students.add(new Student("Anshul", 29));
students.add(new Student("Sneha", 22));
// Original List
System.out.println("Original List:");
// Iterating List
for (Student it : students) {
System.out.println(it);
}
System.out.println();
// Sort students by name, then by age
students.sort(Comparator.comparing(Student::getName).thenComparing(Student::getAge));
// Display message after sorting
System.out.println("After Sorting:");
// Iterating using enhanced for-loop after sorting ArrayList
for (Student it : students) {
System.out.println(it);
}
}
}
Output
Original List: Ajay : 27 Sneha : 23 Simran : 37 Ankit : 22 Anshul : 29 Sneha : 22 After Sorting: Ajay : 27 Ankit : 22 Anshul : 29 Simran : 37 Sneha : 22 Sneha : 23
Comparator vs Comparable
Feature | Comparator | Comparable |
---|---|---|
Sorting Logic Location | Defined externally | Defined within the class (Internally) |
Multiple Sorting Orders | Supported | Not supported |
Interface Methods | compare() | compareTo() |
Functional Interface | Yes | No |
Usage | Flexible and reusable | Simple and tightly coupled |