Java Comparable and Comparator – Complete Guide with Examples
Learn how to sort objects in Java using Comparable and Comparator interfaces, their differences, methods, and practical examples for custom sorting of collections.
Comparable and Comparator in Java – Complete Detailed Tutorial
In Java, sorting objects requires defining order. The Comparable and Comparator interfaces provide ways to define natural and custom ordering for objects.
1. Comparable Interface
- Defines natural ordering for objects
- Part of java.lang package
- Used with Collections.sort() or Arrays.sort()
- Implements
compareTo()method
Syntax:
public int compareTo(T obj)
- Returns negative → current object < obj
- Returns zero → current object == obj
- Returns positive → current object > obj
Example – Using Comparable
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
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 s) {
return this.age - s.age; // ascending order by age
}
@Override
public String toString() {
return name + " (" + age + ")";
}
}
public class Main {
public static void main(String[] args) {
List<Student> students = new ArrayList<>();
students.add(new Student("Alice", 22));
students.add(new Student("Bob", 20));
students.add(new Student("Charlie", 25));
Collections.sort(students); // uses compareTo()
System.out.println("Sorted by age: " + students);
}
}
Output:
Sorted by age: [Bob (20), Alice (22), Charlie (25)]
Key Points:
- Comparable is implemented by the class itself
- Defines natural ordering
- Only one sorting sequence possible
2. Comparator Interface
- Defines custom ordering for objects
- Part of java.util package
- Can be separate class or anonymous class
- Implements
compare()method
Syntax:
public int compare(T o1, T o2)
- Returns negative → o1 < o2
- Returns zero → o1 == o2
- Returns positive → o1 > o2
Example – Using Comparator
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
class Student {
String name;
int age;
Student(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return name + " (" + age + ")";
}
}
class NameComparator implements Comparator<Student> {
@Override
public int compare(Student s1, Student s2) {
return s1.name.compareTo(s2.name); // ascending order by name
}
}
public class Main {
public static void main(String[] args) {
List<Student> students = new ArrayList<>();
students.add(new Student("Alice", 22));
students.add(new Student("Bob", 20));
students.add(new Student("Charlie", 25));
Collections.sort(students, new NameComparator());
System.out.println("Sorted by name: " + students);
}
}
Output:
Sorted by name: [Alice (22), Bob (20), Charlie (25)]
Key Points:
- Comparator is a separate class
- Can define multiple sorting sequences
- Useful when class cannot implement Comparable
3. Comparable vs Comparator
| FeatureComparableComparator | ||
| Location | Implemented by class itself | Separate class or lambda |
| Method | compareTo() | compare() |
| Multiple sort sequences | No | Yes |
| Package | java.lang | java.util |
| Sorting | Natural order | Custom order |
4. Key Points
- Use Comparable for default/natural sorting
- Use Comparator for custom or multiple sort criteria
- Works with Collections.sort(), Arrays.sort()
5. Summary
- Comparable: class defines its own natural ordering
- Comparator: external class defines custom ordering
- Both are essential for sorting Java objects efficiently