When should a class be Comparable and/or Comparator?

Nick Heiner picture Nick Heiner · Sep 17, 2009 · Viewed 221k times · Source

I have seen classes which implement both Comparable and Comparator. What does this mean? Why would I use one over the other?

Answer

Dan picture Dan · Sep 17, 2009

The text below comes from Comparator vs Comparable

Comparable

A comparable object is capable of comparing itself with another object. The class itself must implements the java.lang.Comparable interface in order to be able to compare its instances.

Comparator

A comparator object is capable of comparing two different objects. The class is not comparing its instances, but some other class’s instances. This comparator class must implement the java.util.Comparator interface.