← Back to Questions
Java

Difference between Comparable vs Comparator with examples?

Learn Difference between Comparable vs Comparator with examples? with simple explanations, real-time examples, interview tips and practical use cases.

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.

Why this Java question is important?

This interview question helps candidates understand real-time backend development concepts, practical problem solving, coding fundamentals, system design basics and production-ready application behavior.

Practice this question carefully for Java backend roles, Spring Boot developer interviews, microservices interviews, company interviews and full-stack developer preparation.

About the Author

Naresh Kumar is a Senior Java Backend Engineer with experience building enterprise applications using Java, Spring Boot, Microservices, Docker, Kubernetes and Cloud technologies.