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.
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.
Because Employee is Comparable, Collections.sort(staff), List.sort(null), Arrays.sort, TreeSet and TreeMap all know how to order it without extra arguments.
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.
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.
| Aspect | Comparable | Comparator |
|---|---|---|
| Package | java.lang | java.util |
| Method | compareTo(T other) | compare(T a, T b) |
| Where it is written | Inside the class being sorted | In a separate class, lambda or factory call |
| Number of orderings | One (the natural order) | As many as you like |
| Modifies the class | Yes | No |
| Works for third party classes | No | Yes |
| Typical use | Default order such as ID, date or name | Sort by other fields, reverse, multi-field |
Modern Java gives you Comparator.comparing, thenComparing, reversed, nullsFirst and nullsLast, so you rarely need to write the compare body by hand.
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.
Both interfaces share the same contract rules, and breaking them causes confusing bugs in sorted collections.
sign(compare(a, b)) must equal -sign(compare(b, a)).a is before b and b is before c, then a is before c.equals when you can. A TreeSet treats two items as duplicates when the comparison returns zero, even if equals says they differ.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:
Add a tie breaker such as thenComparing(Person::name) to keep both.
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:
Collator) or user settings.You can also combine them: implement Comparable for the default order and offer static Comparator constants for the alternatives.
Technically yes, but it is rarely a good idea. Keep Comparable on the class and expose comparators as separate static fields or factory methods.
The elements must implement Comparable; otherwise you get a ClassCastException at run time. Passing a Comparator overrides the natural order.
No. Any negative, zero or positive int is valid. Only the sign matters, so never compare the result to -1 or 1.
Wrap the key comparator: Comparator.comparing(Person::nickname, Comparator.nullsLast(Comparator.naturalOrder())).
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.