Java Comparator
Java Comparator Interface
Last Updated : 29 Mar 2026
Java Comparator is used to sort objects in different ways by defining custom comparison logic.
In this chapter, you will learn about the Java Comparator interface, its methods, and how to use it to sort objects based on different attributes.
What is Java Comparator Interface?
The Comparator is an interface in the java.util package that is used to compare and sort objects of a user-defined class. It provides methods like compare() and equals() to define custom sorting logic.
Unlike Comparable, Comparator allows multiple sorting sequences that means you can sort objects based on different data members such as name, age, or roll number.
Comparator Interface Declaration
The following is the declaration of the java.util.Comparator interface.
Methods of Comparator Interface
The Comparator interface provides methods to compare two objects and define custom sorting logic for collections. The following table lists commonly used methods:
| Method | Description |
|---|---|
| public int compare(Object obj1, Object obj2) | It compares the first object with the second object. |
| public boolean equals(Object obj) | It is used to compare the current object with the specified object. |
| public boolean equals(Object obj) | It is used to compare the current object with the specified object. |
Using Comparator with Collections.sort()
The Collections class provides static methods to sort elements of a collection. For Set and Map types, sorting is maintained automatically using TreeSet and TreeMap. For List elements, sorting can be done using the Collections.sort() method.
Method of Collections Class for Sorting List Elements:
This method is used to sort the elements of a List based on the custom sorting logic defined by the Comparator.
Java Comparator Example (Non-generic Old Style)
Let's see the example of sorting the elements of List on the basis of age and name. In this example, we have created 4 java classes:
- Student.java
- AgeComparator.java
- NameComparator.java
- Simple.java
Student.java
This class contains three fields rollno, name and age and a parameterized constructor.
AgeComparator.java
This class defines comparison logic based on the age. If the age of the first object is greater than the second, we are returning a positive value. It can be anyone such as 1, 2, 10. If the age of the first object is less than the second object, we are returning a negative value, it can be any negative value, and if the age of both objects is equal, we are returning 0.
NameComparator.java
This class provides comparison logic based on the name. In such case, we are using the compareTo() method of String class, which internally provides the comparison logic.
Simple.java
In this class, we are printing the values of the object by sorting on the basis of name and age.
Output:
Sorting by Name
106 Ajay 27
105 Jai 21
101 Vijay 23
Sorting by age
105 Jai 21
101 Vijay 23
106 Ajay 27
Java Comparator Example (Generic)
Let's see the example of sorting the elements of a List using the Comparator interface with generics. In this example, we have created 4 Java classes:
- Student.java
- AgeComparator.java
- NameComparator.java
- Simple.java
Student.java
AgeComparator.java
NameComparator.java
This class provides comparison logic based on the name. In such case, we are using the compareTo() method of String class, which internally provides the comparison logic.
Simple.java
In this class, we are printing the values of the object by sorting on the basis of name and age.
Output:
Sorting by Name
106 Ajay 27
105 Jai 21
101 Vijay 23
Sorting by age
105 Jai 21
101 Vijay 23
106 Ajay 27
Java 8 Comparator interface
Java 8 Comparator interface is a functional interface that contains only one abstract method. It can be used as a target for lambda expressions or method references.
Methods of Java 8 Comparator Interface
The following table lists the commonly used methods of the Java 8 Comparator interface along with their descriptions:
| Method | Description |
|---|---|
| int compare(T o1, T o2) | It compares the first object with second object. |
| static <T,U extends Comparable<? super U>> Comparator<T> comparing(Function<? super T,? extends U> keyExtractor) | It accepts a function that extracts a Comparable sort key from a type T, and returns a Comparatorthat compares by that sort key. |
| static <T,U> Comparator<T> comparing(Function<? super T,? extends U> keyExtractor, Comparator<? super U> keyComparator) | It accepts a function that extracts a sort key from a type T, and returns a Comparatorthat compares by that sort key using the specified Comparator. |
| static <T> Comparator<T> comparingDouble(ToDoubleFunction<? super T> keyExtractor) | It accepts a function that extracts a double sort key from a type T, and returns a Comparatorthat compares by that sort key. |
| static <T> Comparator<T> comparingInt(ToIntFunction<? super T> keyExtractor) | It accepts a function that extracts an int sort key from a type T, and returns a Comparatorthat compares by that sort key. |
| static <T> Comparator<T> comparingLong(ToLongFunction<? super T> keyExtractor) | It accepts a function that extracts a long sort key from a type T, and returns a Comparatorthat compares by that sort key. |
| boolean equals(Object obj) | It is used to compare the current object with the specified object. |
| static <T extends Comparable<? super T>> Comparator<T> naturalOrder() | It returns a comparator that compares Comparable objects in natural order. |
| static <T> Comparator<T> nullsFirst(Comparator<? super T> comparator) | It returns a comparator that treats null to be less than non-null elements. |
| static <T> Comparator<T> nullsLast(Comparator<? super T> comparator) | It returns a comparator that treats null to be greater than non-null elements. |
| default Comparator<T> reversed() | It returns comparator that contains reverse ordering of the provided comparator. |
| static <T extends Comparable<? super T>> Comparator<T> reverseOrder() | It returns comparator that contains reverse of natural ordering. |
| default Comparator<T> thenComparing(Comparator<? super T> other) | It returns a lexicographic-order comparator with another comparator. |
| default <U extends Comparable<? super U>> Comparator<T> thenComparing(Function<? super T,? extends U> keyExtractor) | It returns a lexicographic-order comparator with a function that extracts a Comparable sort key. |
| default <U> Comparator<T> thenComparing(Function<? super T,? extends U> keyExtractor, Comparator<? super U> keyComparator) | It returns a lexicographic-order comparator with a function that extracts a key to be compared with the given Comparator. |
| default Comparator<T> thenComparingDouble(ToDoubleFunction<? super T> keyExtractor) | It returns a lexicographic-order comparator with a function that extracts a double sort key. |
| default Comparator<T> thenComparingInt(ToIntFunction<? super T> keyExtractor) | It returns a lexicographic-order comparator with a function that extracts a int sort key. |
| default Comparator<T> thenComparingLong(ToLongFunction<? super T> keyExtractor) | It returns a lexicographic-order comparator with a function that extracts a long sort key. |
Java 8 Comparator Example
Let's see the example of sorting the elements of List on the basis of age and name.
File: Student.java
File: TestSort1.java
Output:
Sorting by Name 106 Ajay 27 105 Jai 21 101 Vijay 23 Sorting by Age 105 Jai 21 101 Vijay 23 106 Ajay 27
Another Example: Java 8 Comparator nullsFirst() and nullsLast() Methods
Here, we sort the list of elements that also contains null.
File: Student.java
File: TestSort2.java
Output:
Considers null to be less than non-null 105 null 21 106 Ajay 27 101 Vijay 23 Considers null to be greater than non-null 106 Ajay 27 101 Vijay 23 105 null 21