← Back to Home

Comparable vs Comparator

Comparable and Comparator are used in Java to define sorting logic for objects. They answer two different questions:

  • Comparable: How should this object be compared to another of the same type?
  • Comparator: How should two objects be compared using an external rule?

This is a very high-frequency interview topic.

What Is Comparable?

Comparable is an interface that defines natural ordering of objects.

public interface Comparable<T> {
    int compareTo(T o);
}
          

Key Points

  • Implemented inside the class
  • Defines default / natural order
  • Affects TreeSet, TreeMap, Collections.sort()
  • Only one sorting logic per class

Comparable Example

class Student implements Comparable<Student> {
    int id;
    String name;

    Student(int id, String name) {
        this.id = id;
        this.name = name;
    }

    @Override
    public int compareTo(Student s) {
        return this.id - s.id; // sort by id
    }
}

List<Student> list = new ArrayList<>();
Collections.sort(list);
          

✔ Sorting happens automatically

✔ Uses compareTo()

What Is Comparator?

Comparator is a separate object that defines custom sorting logic.

public interface Comparator<T> {
    int compare(T o1, T o2);
}
          

Key Points

  • Implemented outside the class
  • Allows multiple sorting strategies
  • Used when class cannot be modified
  • Preferred for flexibility

Comparator Example

class NameComparator implements Comparator<Student> {
    @Override
    public int compare(Student s1, Student s2) {
        return s1.name.compareTo(s2.name);
    }
}

Collections.sort(list, new NameComparator());
          

✔ Sorts by name

✔ Class remains unchanged

Comparator Using Lambda (Java 8+)

Collections.sort(list, (s1, s2) -> s1.name.compareTo(s2.name));
          

✔ Clean

✔ Concise

✔ Modern approach

Sorting with TreeSet / TreeMap

Using Comparable

TreeSet<Student> set = new TreeSet<>();
          

✔ Uses compareTo()

Using Comparator

TreeSet<Student> set =
    new TreeSet<>((a, b) -> a.name.compareTo(b.name));
          

✔ Uses custom logic

Comparable vs Comparator (Interview Favorite Table)

Aspect Comparable Comparator
Package java.lang java.util
Method compareTo() compare()
Sorting logic Inside class Outside class
Number of strategies One Multiple
Class modification Required Not required
Flexibility Less More

Return Values of compare / compareTo (Important)

Return Value Meaning
0 Objects equal
< 0 First object smaller
> 0 First object greater

Real-World Analogy

Comparable

Student knows how to compare itself with another student (by roll number)

Comparator

External examiner compares students by name, marks, or age

Common Beginner Mistakes

  • Confusing equals() with compareTo()
  • Returning wrong comparison values
  • Not handling null values
  • Using subtraction for large numbers (overflow risk)
  • Forgetting consistency with equals()

Best Practices

  • Use Comparable for natural/default order
  • Use Comparator for alternate sorting
  • Prefer Comparator.comparing() (Java 8+)
  • Ensure comparison logic is consistent with equals()
  • Avoid subtraction for comparison (use Integer.compare())
return Integer.compare(this.id, s.id);
          

Interview-Ready Answers

Short Answer

Comparable is used for natural ordering, while Comparator is used for custom ordering.

Detailed Answer

In Java, Comparable defines a class’s natural ordering using the compareTo() method, whereas Comparator defines external sorting logic using the compare() method. Comparable allows only one sorting order, while Comparator supports multiple sorting strategies and greater flexibility.

Key Takeaway

Comparable = natural order (inside class). Comparator = custom order (outside class). Use Comparable for default sorting and Comparator for flexible, multiple sorting rules.