Open In App

Java Comparator Interface

Last Updated : 31 Jul, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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?

  • Use when sorting logic should not be inside the class.
  • when you want to sort by multiple fields like name, roll number, age

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


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)


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:


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

ComparatorComparable
Sorting logic is defined externally.Sorting logic is defined within the class (Internally)
Supports multiple sorting orders.Does not support multiple sorting orders.
Uses the compare() methodUses the compareTo() method.
It is a functional interface.It is also a functional interface (since Java 8)
More flexible and reusableSimple and tightly coupled

Comparator Interface
Visit Course explore course icon
Article Tags :
Practice Tags :

Similar Reads