Comparable and Comparator

Comparable<T> defines a natural ordering for a class via compareTo, used by Collections.sort and TreeSet. Comparator<T> defines an external ordering, or several, via compare. Both return a negative, zero, or positive integer depending on whether the first is less than, equal to, or greater than the second.

Example

class Personne implements Comparable<Personne> {
    public int compareTo(Personne o) { return nom.compareTo(o.nom); }
}

Related concepts

The whole glossary