Comparable
is usually preferred. But sometimes a class already implements Comparable
, but you want to sort on a different property. Then you're forced to use a Comparator
.
Some classes actually provide Comparators
for common cases; for instance, String
s are by default case-sensitive when sorted, but there is also a static Comparator
called CASE_INSENSITIVE_ORDER
.