Sorting interfaces
Comparable<T> and Comparator<T> are the standard interfaces used by all standard Java sorting options (all except those related to primitive arrays).
In Java-talk, any class that implements Comparable has a "natural order". (Technically, he should implement Comparable<? super E> , where E matches itself, to have a natural order). Classes that do not have a natural order can be sorted using an external comparator. The comparator can also be used to implement a sort order that runs against the natural order (for example, sorting strings in descending alphabetical order).
These principles apply to both SortedSet<E> and SortedMap<K,V> , which sort their elements / keys automatically, using either the natural order or the supplied comparator.
The same principles can also be found in the static methods of the Utility.
which can be used to sort arrays and lists, respectively, each of which does not support sorting.
guavas
The Guava library takes this a bit further by providing abstract Ordering as a basic Comparator implementation that already contains standard methods, such as providing a reverse view of yourself and many convenient methods for sorting and retrieving based on this Ordering .
Link
A good article on Ordering Objects can be found in the Sun Java Trail Collection Tutorial .
source share