Comparable vs Comparator
Fast recall
Quick revision
Core answer
compareTo(T) implemented inside the class itself (one ordering). Comparator is an external ordering strategy via compare(T, T), defined outside the class, so you can have many orderings and sort types you don't own. Use Comparable for the default sort, Comparator for custom or multiple sorts.Whether you know which interface lives inside vs outside the class and when to use each
Mixing up compareTo (one arg) with compare (two args), or thinking you can only sort with Comparable
Show modern Comparator chaining: comparing(...).thenComparing(...).reversed()
Interview-ready guide
Interview answer
Comparable and Comparator both define ordering, but they live in different places and serve different needs.
Comparable is implemented inside the class itself and defines the type's natural ordering. Its method is compareTo, which takes a single argument — the other object — and returns a negative number if this object is smaller, zero if they're equal, or a positive number if it's larger.
For example, String sorts alphabetically and Integer sorts numerically because they implement Comparable. The key limitation is that a type has exactly one natural order. Comparator is different: it's a separate object that defines ordering externally.
Its method is compare, which takes two arguments and compares them. Because it lives outside the class, you can create as many comparators as you like — sort employees by name, by age, by salary — and you can even sort classes whose source code you don't own, which you can't do with Comparable.
So the rule of thumb is: if there's one obvious default ordering and you own the class, make it Comparable. For anything situational — custom rules, multiple sort orders, descending order, or third-party types — pass a Comparator. A nice modern detail to mention is comparator chaining.
comparing by one field, then thenComparing for a tiebreaker, and reversed to flip the direction, all in one readable expression. The classic mistakes are mixing up the method signatures — compareTo takes one argument, compare takes two — and thinking you can only sort with Comparable, when Comparator is actually the more flexible tool.
Comparable vs Comparator
Direct Answer
Comparable defines a type's natural ordering via compareTo(T) implemented inside the class itself (one ordering). Comparator is an external ordering strategy via compare(T, T), defined outside the class, so you can have many orderings and sort types you don't own. Use Comparable for the default sort, Comparator for custom or multiple sorts.
Common Mistakes & Pitfalls
- Swapping the method signatures.
compareTotakes one argument (the other object);comparetakes two. Mixing them up will not compile. - Thinking you can only sort with
Comparable.Comparatoris the more flexible tool — many orderings, and it works on types you do not own. - Returning booleans instead of negative/zero/positive. Both methods return an
int: negative, zero, or positive — nottrue/false. - Forgetting
TreeSet/TreeMapneed an ordering. They use natural order viaComparable, or a suppliedComparator; without either you get aClassCastExceptionat runtime.
Check your understanding
Answer these without looking back at the article.
- How do you sort a list of objects by multiple fields?
- Can TreeSet use a Comparator instead of Comparable?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.