Best Division

Spread Your Knowledge

  • All topics
  • About us
  • Contact us
  • Privacy policy
  • Terms of service
  • Copyright
  • Facebook
  • Twitter
  • Instagram
  • Youtube
  • LinkedIn
  • Pinterest
  • Github
  • Tumblr

© 2026 Best Division

What Is the Difference Between Comparable and Comparator in Java?

JavaInterview Questions
Jayant Kumar
Jayant Kumar@jayantkumar314
PublishedDec 5, 2026
7 min read

Comparable defines the natural ordering of a class from inside the class, through a single compareTo method. Comparator defines an ordering from outside the class, through a compare method, so you can have as many orderings as you need without changing the class. Use Comparable for the one obvious default order, and Comparator for every other order, for classes you cannot edit, and for multi-field sorting.

In short, the difference between comparable vs comparator comes down to where the ordering lives: in the type itself, or in a separate object that you pass to sort, TreeSet, TreeMap or a stream.

Table of Contents

  • What Comparable does
  • What Comparator does
  • Side by side comparison
  • Sorting by several fields with Comparator
  • Getting the contract right
  • When to choose each one
  • Frequently Asked Questions
    • Can a class implement both Comparable and Comparator?
    • Which is used when I call Collections.sort with no comparator?
    • Does Comparator.compare have to return exactly -1, 0 or 1?
    • How do I sort with null values safely?
  • Summary
  • Related Articles

What Comparable does

java.lang.Comparable<T> has one method, int compareTo(T other). A class that implements it declares a natural order. Classes such as String, Integer, LocalDate and BigDecimal already do.

The method returns a negative number if this comes before other, zero if they are equal in ordering, and a positive number if this comes after.

java
text

Because Employee is Comparable, Collections.sort(staff), List.sort(null), Arrays.sort, TreeSet and TreeMap all know how to order it without extra arguments.

What Comparator does

java.util.Comparator<T> is a functional interface with one abstract method, int compare(T a, T b). It lives outside the class being compared, so you can create it as a lambda, a method reference chain or a separate class.

java
text

Employee here does not implement anything. The ordering is supplied by the caller, which is why Comparator works for classes from libraries you cannot modify.

Side by side comparison

AspectComparableComparator
Packagejava.langjava.util
MethodcompareTo(T other)compare(T a, T b)
Where it is writtenInside the class being sortedIn a separate class, lambda or factory call
Number of orderingsOne (the natural order)As many as you like
Modifies the classYesNo
Works for third party classesNoYes
Typical useDefault order such as ID, date or nameSort by other fields, reverse, multi-field

Sorting by several fields with Comparator

Modern Java gives you Comparator.comparing, thenComparing, reversed, nullsFirst and nullsLast, so you rarely need to write the compare body by hand.

java
text

The chain sorts by department, then by salary descending, then by name to break ties. The comparator is built once and can be reused or stored in a constant.

Getting the contract right

Both interfaces share the same contract rules, and breaking them causes confusing bugs in sorted collections.

  1. Be consistent: sign(compare(a, b)) must equal -sign(compare(b, a)).
  2. Be transitive: if a is before b and b is before c, then a is before c.
  3. Stay consistent with equals when you can. A TreeSet treats two items as duplicates when the comparison returns zero, even if equals says they differ.
  4. Do not subtract integers to compare them. a.age - b.age can overflow; use Integer.compare(a.age, b.age).

The TreeSet point is a classic pitfall. If you compare employees only by age, two employees of the same age collapse into one entry:

java
text

Add a tie breaker such as thenComparing(Person::name) to keep both.

When to choose each one

Choose Comparable when the class has a single, obvious, unambiguous order that every user would expect, such as a version number or a money amount. Choose Comparator when:

  • the class is not yours, or you do not want to change it;
  • you need several orderings, for example by name in one screen and by date in another;
  • you need descending or case insensitive order;
  • the order depends on context, such as a locale (Collator) or user settings.

You can also combine them: implement Comparable for the default order and offer static Comparator constants for the alternatives.

java

Frequently Asked Questions

Can a class implement both Comparable and Comparator?

Technically yes, but it is rarely a good idea. Keep Comparable on the class and expose comparators as separate static fields or factory methods.

Which is used when I call Collections.sort with no comparator?

The elements must implement Comparable; otherwise you get a ClassCastException at run time. Passing a Comparator overrides the natural order.

Does Comparator.compare have to return exactly -1, 0 or 1?

No. Any negative, zero or positive int is valid. Only the sign matters, so never compare the result to -1 or 1.

How do I sort with null values safely?

Wrap the key comparator: Comparator.comparing(Person::nickname, Comparator.nullsLast(Comparator.naturalOrder())).

Summary

Comparable gives a class one built in natural order through compareTo. Comparator supplies any number of external orderings through compare, and the factory methods comparing, thenComparing and reversed make them short to write. Keep both consistent with equals where possible, avoid integer subtraction, and use a comparator whenever you cannot or should not change the class.

Related Articles

  • What is the Comparator interface in Java, and how does it work?
  • What is the purpose of the Collections utility class in Java?
  • Learn Java: Complete Study Roadmap from Beginner to Advanced
Share