Comparable and Comparator interface
1. Comparable Interface
- The Comparable interface is used to define the natural ordering of objects of a class. It is typically implemented by the class whose instances are intended to be sorted.
- The Comparable interface defines a single method, compareTo(), which compares the current object with another object and returns a negative integer, zero, or a positive integer based on whether the current object is less than, equal to, or greater than the specified object, respectively Comparable and Comparator interface.
- Implementing the Comparable interface allows objects of a class to be sorted using methods like Collections.sort() or Arrays.sort().
2. Comparator Interface
- The Comparator interface is used to define custom ordering of objects that may not have a natural ordering or when you want to sort objects based on criteria other than their natural order.
- The Comparator interface defines a single method, compare(), which takes two objects as arguments and returns a negative integer, zero, or a positive integer based on whether the first object is less than, equal to, or greater than the second object, respectively Comparable and Comparator interface.
- Comparator objects can be used to sort collections using methods like Collections.sort() or Arrays.sort(), allowing for flexibility in sorting criteria.

Table of Contents
Example - Comparable Interface:
java
import java.util.ArrayList;
import java.util.Collections;
// Student class implementing Comparable interface
class Student implements Comparable<Student> {
private int id;
private String name;
public Student(int id, String name) {
this.id = id;
this.name = name;
}
// Implementing compareTo method for natural ordering
@Override
public int compareTo(Student other) {
return this.id - other.id;
}
// Getter methods
public int getId() {
return id;
}
public String getName() {
return name;
}
}
public class ComparableExample {
public static void main(String[] args) {
// Create a list of students
ArrayList<Student> students = new ArrayList<>();
students.add(new Student(101, "Alice"));
students.add(new Student(103, "Bob"));
students.add(new Student(102, "Charlie"));
// Sort the list using natural ordering (compareTo method)
Collections.sort(students);
// Print the sorted list
for (Student student : students) {
System.out.println("ID: " + student.getId() + ", Name: " + student.getName());
}
}
}
Example - Comparator Interface:
java
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
// Student class without implementing Comparable interface
class Student {
private int id;
private String name;
public Student(int id, String name) {
this.id = id;
this.name = name;
}
// Getter methods
public int getId() {
return id;
}
public String getName() {
return name;
}
}
public class ComparatorExample {
public static void main(String[] args) {
// Create a list of students
ArrayList<Student> students = new ArrayList<>();
students.add(new Student(101, "Alice"));
students.add(new Student(103, "Bob"));
students.add(new Student(102, "Charlie"));
// Sort the list using a custom Comparator
Collections.sort(students, new Comparator<Student>() {
@Override
public int compare(Student s1, Student s2) {
return s1.getName().compareTo(s2.getName());
}
});
// Print the sorted list
for (Student student : students) {
System.out.println("ID: " + student.getId() + ", Name: " + student.getName());
}
}
}
Example
In the first example using the Comparable and Comparator interface the Student class implements the Comparable interface, allowing sorting based on the student ID. In the second example using the Comparator interface, we sort the list of students based on their names, without modifying the Student class.