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 :)
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.
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.
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.
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.
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.
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.
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.
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.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With