public interface Comparable4D<T>
Lists, arrays, and sets of objects that implement this interface can
be sorted automatically, without the need for an explicit
Comparator. Note that e1.compareTo(null)
should throw an Exception; as should comparison between incompatible
classes.
Comparator,
Collections.sort(java.util.List),
Arrays.sort(Object[]),
SortedSet,
SortedMap,
TreeSet,
TreeMap| Modifier and Type | Method and Description |
|---|---|
int |
compareTo(T o)
Compares this object with another, and returns a numerical result based
on the comparison.
|
int compareTo(T o)
o1.compareTo(o2) <op> 0, where op
is one of <, <=, =, !=, >, or >=.
You must make sure that the comparison is mutual, ie.
sgn(x.compareTo(y)) == -sgn(y.compareTo(x)) (where sgn() is
defined as -1, 0, or 1 based on the sign). This includes throwing an
exception in either direction if the two are not comparable; hence,
compareTo(null) should always throw an Exception.
You should also ensure transitivity, in two forms:
x.compareTo(y) > 0 && y.compareTo(z) > 0 implies
x.compareTo(z) > 0; and x.compareTo(y) == 0
implies x.compareTo(z) == y.compareTo(z).
o - the object to be comparedjava.lang.NullPointerException - if o is nulljava.lang.ClassCastException - if o cannot be compared