Difference Between Comparable vs Comparator in Java
Introduction
Sorting objects is a common requirement in Java applications. The language provides two main ways to define sorting logic:
Comparable and Comparator. While both are used to compare objects, they differ in approach, flexibility, and usage.
Understanding these differences is crucial for interviews and real-world development.
Comparable Interface
The Comparable interface is used to define the natural ordering of objects.
A class implements Comparable and overrides the compareTo() method.
// Example: Comparable
class Student implements Comparable<Student> {
String name;
int age;
Student(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public int compareTo(Student other) {
return this.age - other.age; // sort by age
}
}
public class Main {
public static void main(String[] args) {
List<Student> list = new ArrayList<>();
list.add(new Student("Alice", 22));
list.add(new Student("Bob", 20));
list.add(new Student("Charlie", 25));
Collections.sort(list); // uses compareTo
for(Student s : list) {
System.out.println(s.name + " - " + s.age);
}
}
}
Comparator Interface
The Comparator interface is used to define custom ordering.
It is implemented separately and overrides the compare() method.
This allows multiple sorting strategies without modifying the original class.
// Example: Comparator
class Student {
String name;
int age;
Student(String name, int age) {
this.name = name;
this.age = age;
}
}
class NameComparator implements Comparator<Student> {
@Override
public int compare(Student s1, Student s2) {
return s1.name.compareTo(s2.name); // sort by name
}
}
public class Main {
public static void main(String[] args) {
List<Student> list = new ArrayList<>();
list.add(new Student("Alice", 22));
list.add(new Student("Bob", 20));
list.add(new Student("Charlie", 25));
Collections.sort(list, new NameComparator()); // uses compare
for(Student s : list) {
System.out.println(s.name + " - " + s.age);
}
}
}
Comparison Table
| Aspect | Comparable | Comparator |
|---|---|---|
| Method | compareTo(T o) |
compare(T o1, T o2) |
| Package | java.lang |
java.util |
| Ordering | Defines natural ordering | Defines custom ordering |
| Flexibility | Only one ordering per class | Multiple orderings possible |
| Modification | Requires changing the class | No need to modify the class |
When to Use Each
- Use Comparable: When you want a single, natural ordering (e.g., sorting students by roll number).
- Use Comparator: When you need multiple sorting strategies (e.g., sort students by name, age, or marks).
Interview-Ready Notes
In interviews, emphasize that Comparable is for natural ordering and requires modifying the class,
while Comparator is for custom ordering and allows multiple strategies.
A strong answer connects the concept to real-world scenarios:
βFor a Student class, Iβd use Comparable to sort by roll number (natural ordering).
If I need to sort by name or age, Iβd use Comparator to define different strategies without changing the Student class.β
Conclusion
Both Comparable and Comparator are essential for sorting in Java.
Choosing between them depends on whether you need a single natural ordering or multiple custom orderings.
Mastering these interfaces ensures you can design flexible, maintainable, and interview-ready solutions.