Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

difference between natural ordering and total ordering

I happen to come across many statements like comparable is used when natural ordering is required while sorting an array or collection and comparator for total ordering.

The version you may have heard could be same or different with the same meaning but ultimately its one of the distinguishing factors between the two(comparator and comparable interfaces).

But, I couldn't find a difference between the two types of ordering anywhere. I'd appreciate if someone could explain it with a good example :)

like image 691
Nav Avatar asked Feb 07 '12 12:02

Nav


People also ask

What is natural ordering in statistics?

A correct natural sorting algorithm states that you order alphabetically but when you encounter a digit you will order that digit and all the subsequent digits as a single character. Natural sorting has nothing to do with sorting by string length first, and then alphabetically when two strings have the same length.

What is the natural sorting order?

In computing, natural sort order (or natural sorting) is the ordering of strings in alphabetical order, except that multi-digit numbers are treated atomically, i.e., as if they were a single character.

What is natural ordering in TreeSet?

The TreeSet stores the objects in the ascending order, which is a natural ordering of a tree. We can also specify a comparator to sort the elements based on it during the creation of the TreeSet. It implements the SortedSet and NavigableSet interface to maintain and navigate the order of the elements.

What is difference between ordering and sorting?

Very roughly speaking, an ordering specifies which elements should be ordered before which other elements in some sequence. Sorting is the process of putting a collection of such elements into the order specified by an ordering.

What is the difference between total and natural order in Java?

7 Answers 7. Total ordering means all values can be compared to all other values. 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.

What is an example of natural order?

On the concept of natural ordering: If the objects of a type have a really-really obvious way to be sorted, than it is the natural ordering. For example, the natural ordering of Strings is the alphabetical order, and the natural order of numbers is ascending order, because it is the first choice anybody would think of.

What is natural order and comparative order?

Natural Order. It depends on our collections that we use, for example, say, we have characters object, then natural order is their unicode values, for numbers natural order is as usual, ascending order. Comparable Interface- This interface imposes a total ordering on the objects of each class that implements it.

What is meant by total ordering?

Total ordering is any ordering where all values can be compared to all other values. e.g. when you design new class then you can choose the natural ordering inside the class. Any other ordering can be then only the total one ;) What exactly do you mean by all values can be compared to all other values.


2 Answers

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.

like image 59
Peter Lawrey Avatar answered Oct 14 '22 08:10

Peter Lawrey


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).

like image 40
Bruno Avatar answered Oct 14 '22 08:10

Bruno