difference between natural ordering and total ordering
Total ordering means all values can be compared to all other values. For example, if you have a collection of BigDecimal
and String
there is no natural total order (but you could invent one)
In Java, the Natural order is defined as the ordering provided by the JVM. This might not match what a people might believe is the natural order. e.g. Strings are sorted ASCIIbetically. Meaning an uppercase Z
comes before a lowercase a
and 10
is before 2
http://docs.oracle.com/javase/7/docs/api/java/lang/Comparable.html
This interface imposes a total ordering on the objects of each class that implements it. This ordering is referred to as the class's natural ordering, and the class's compareTo method is referred to as its natural comparison method.
Total ordering is a general mathematical concept. It differs mainly from partial ordering in that for each a and b in set X, either "a <= b" or "b <= a" are meaningful and true. As far as Java is concerned, this means that of two Comparable
instances, one must be greater or equal than the other (i.e. it makes sense to compare them).