Java Comparator Interface

Introduction:
You've probably studied how to sort simple lists in Java, both alphabetically and numerically. What occurs, though, if your list includes items like employees or students?
You must establish a comparison rule in order to sort these items. For instance, you could make the following rule if you wish to arrange students according to age: "Students with smaller ages come first."
Java's Comparator and Comparable interfaces are useful in this situation.
You have complete control over the organization of data thanks to these APIs, which allow you to modify the sorting logic for objects, strings, and integers.
Comparators
An object that implements the Comparator interface is called a comparator.
The Comparator interface allows you to create a class with a compare() method that compares two objects to decide which one should go first in a list.
The compare() method should return a number that is
Negative if the first object should go first in a list.
Positive if the second object should go first in a list.
Zero if the order does not matter.
Java Example: Sorting Students by Age
Here’s a working example where we sort a list of Student
objects by their age using a custom comparator:
import java.util.*;
// Define a Student class
class Student {
int id;
String name;
int age;
Student(int id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}
}
// Create a comparator
class SortByAge implements Comparator {
public int compare(Object obj1, Object obj2) {
// Make sure that the objects are Student objects
Student s1 = (Student) obj1;
Student s2 = (Student) obj2;
// Compare the age of both objects
if (s1.age < s2.age) return -1; // The first student has a smaller age
if (s1.age > s2.age) return 1; // The first student has a larger age
return 0; // Both students have the same age
}
}
class TestComparator {
public static void main(String[] args) {
// Create a list of Students
ArrayList<Student> myStudent = new ArrayList<Student>();
myStudent.add(new Student(77, "Keerti", 18));
myStudent.add(new Student(103, "Piyush", 20));
myStudent.add(new Student(85, "Lucky", 19));
// Use a comparator to sort the Students
Comparator myComparator = new SortByAge();
Collections.sort(myStudent, myComparator);
// Display the Students
for (Student Stu : myStudent) {
System.out.println(Stu.id + " " + Stu.name + " " + Stu.age);
}
}
}
Output:
77 Keerti 18
85 Lucky 19
103 Piyush 20
Special Sorting Rules (Advanced)
You can use comparators for more than just numbers—you can apply custom sorting rules like:
Sorting even numbers before odd numbers
Sorting strings by length
Sorting names in reverse alphabetical order
Conclusion
The Comparator
interface is a powerful way to control how collections of objects are sorted in Java. Whether you’re building student databases, ranking results, or filtering lists—mastering this concept is essential for writing clean, flexible Java code.
Subscribe to my newsletter
Read articles from Imran Ansari directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
