VOOZH about

URL: https://www.geeksforgeeks.org/java/java-comparator-interface/

⇱ Java Comparator Interface - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Java Comparator Interface

Last Updated : 13 May, 2026

The Comparator interface in Java is used to define custom sorting logic for objects. It belongs to java.util package allows sorting of objects of user-defined classes without modifying their source code. It is especially useful when:

  • We need multiple sorting strategies for a class.
  • We want to keep sorting logic separate from the class definition.
  • Comparator is a functional interface as it contains only one abstract method compare().

Syntax

class MyComparator implements Comparator<Type> {
public int compare(Type obj1, Type obj2) {
// comparison logic
}
}

  • Returns negative integer if obj1 < obj2.
  • Returns 0 if obj1 == obj2.
  • Returns positive integer if obj1 > obj2.

Output
1 Amit
2 Rahul

Explanation:

  • Creates a Student class with id and name.
  • Uses Comparator (lambda expression) to sort students by id.
  • Collections.sort() applies the custom sorting logic.
  • Output is sorted in ascending order of id.

Methods in Comparator Interface

How Collections.sort() Works with Comparator

The Collections.sort() method arranges the elements of a List based on the rules defined by a Comparator.

Syntax

public static <T> void sort(List<T> list, Comparator<? super T> c)

The method uses the compare() function of the given Comparator to decide the order of elements. The compare() method compares two elements and returns:

  • Negative value: first element comes before second
  • Zero: both elements are equal
  • Positive value: first element comes after second

Example: Sorting by Multiple Fields (Name, then Age)


Output
Original List:
Ajay : 27
Sneha : 23
Simran : 37

After Sorting:
Ajay : 27
Simran : 37
Sneha : 23

Explanation

  • Student class includes name and age with getter methods.
  • StudentComparator compares by name first, then by age if names are same.
  • Collections.sort() uses this comparator to order students.
  • Final list shows sorted order by both name and age fields.

Note: String comparison using compareTo() is case-sensitive, where uppercase letters are considered smaller than lowercase. For case-insensitive sorting, use compareToIgnoreCase() or Comparator.comparing(String::toLowerCase).

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)

);


Output
Original List:
Ajay : 27
Sneha : 23
Simran : 37

After Sorting:
Ajay : 27
Simran : 37
Sneha : 23

Explanation

  • Comparator.comparing() creates comparator by name.
  • .thenComparing() adds secondary comparator by age.
  • students.sort() sorts list using these chained comparators.
  • The code achieves multi-field sorting in one line — concise and readable.
Comment
Article Tags:
Article Tags: