## difference between natural ordering and total ordering

comparable example in java 8

rules of total ordering java

java natural order comparator

java comparable

comparator java

implements comparable java

comparable vs comparator

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

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 `Z`

comes before `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.

**Java Comparable Example for Natural Order Sorting,** If the List consists of String elements, it will be sorted into alphabetical order. Comparable implementations provide a natural ordering for a class, which allows method for the same reason — a Comparator must induce a total order on the is not big enough to represent the difference of two arbitrary signed integers. The total order you're probably most familiar with is 'less than' on the natural numbers: 1 is less than 2, which is less than 3, and so on. If we draw a total order, it looks just like a long line of objects (again, in the finite case at least).

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

**Object Ordering (The Java™ Tutorials > Collections > Interfaces),** Classes whose objects used in collections or arrays should implement the Comparableinterface for providing the natural ordering of its objects Sorting for Humans : Natural Sort Order. The default sort functions in almost every programming language are poorly suited for human consumption. What do I mean by that? Well, consider the difference between sorting filenames in Windows explorer, and sorting those very same filenames via Array.Sort() code:

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

Lists (and arrays) of objects that implement this interface can be sorted automatically by Collections.sort (and Arrays.sort). Objects that implement this interface can be used as keys in a sorted map or elements in a sorted set, without the need to specify a comparator.

public interface Comparable<T> { /** * Compares this object with the specified object for order. Returns a * negative integer, zero, or a positive integer as this object is less * than, equal to, or greater than the specified object. */ public int compareTo(T o); }

Comparator Interface:

This interface Represents an order relation, which may be used to sort a list or maintain order in a sorted set or map. Can override a type's natural ordering, or order objects of a type that does not implement the Comparable interface.

A comparison function, which imposes a total ordering on "some collection of objects". Comparators can be passed to a sort method (such as Collections.sort) to allow precise control over the sort order. Comparators can also be used to control the order of certain data structures (such as TreeSet or TreeMap).

public interface Comparator<T> { /** * Compares its two arguments for order. Returns a negative integer, * zero, or a positive integer as the first argument is less than, equal * to, or greater than the second. */ int compare(T o1, T o2); boolean equals(Object obj); } Hope This helps you.

**Understanding Object Ordering in Java with Comparable and ,** Natural ordering of elements is imposed by compareTo() method. Java Comparable interface imposes a total ordering on the objects of each class that Technically speaking, these restrictions ensure that the natural ordering is a total order on the objects of a class that implements it; this is necessary to ensure that sorting is well defined. Comparators

*Important point:* **natural** ordering should be **consistent with equals**!

*Summary:* natural ordering is one kind of total ordering which is default (used the most often) for the given class and is consistent with equals. 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 ;)

**Java Comparable interface example,** This total ordering is referred to as the natural ordering. The comparison for ordering is asserted by the compareTo method of this interface, 1. Total cost = holding cost + ordering cost = (order quantity/2) x holding cost per unit per year + (annual demand/order quantity) x cost per order. 2. Optimal order quantity (Q*) is found when annual holding cost = ordering cost 3. Number of orders = Annual Demand/Q* 4. Time between orders = No. of working days per year / number of orders 5.

To elaborate on @Bruno's answer: an example of a **partial ordering** is the **divisibility relation** between positive numbers. If you compare 5 and 15, you can say that 5 is a divisor of 15, and 15 is the multiple of 5. However, 3 and 5 are not comparable because 3 is neither the divisor nor the multiple of 5.

An example of **total ordering** is the **less than relation**, because if you take any two different numbers, one of them is less than the other. So any value is comparable with any other value.

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. However, sometimes you would want to order Strings in a different way, e.g. sorting by length from the 1-character ones to the longer ones. This is a possible total ordering on Strings, but not the natural one.

Not all objects necessarily have a natural ordering. E.g. if you have Person objects, sorting them by height is a possible total ordering, but so is sorting them by age... None of these are more obvious than the other, this is why there is no natural ordering.

**Learning the Difference Between Comparable and Comparator,** For example, consider the set of all subsets of the integers, partially ordered by inclusion. Then the set {In : n is a natural number}, All trades consist of at least two orders: one to get into the trade, and another order to exit the trade. Order types are the same whether trading stocks, currencies or futures. A single order is either a buy order or a sell order, and an order can be used either to enter a trade or to exit a trade.

**Total order,** Comparators are the comparison functions that can be applied to collections of objects or an array to impose a total ordering on them. They can be passed to As the name suggests, total testosterone is the grand total of all the hormone available in the bloodstream. While some testosterone tests only account for total testosterone, they might not be as helpful as once thought. Here’s why. A 98% proportion is normal for bound testosterone –

**Natural Order Comparators in Java,** Also, we often need to specify a total ordering if there is no natural ordering You define the natural ordering for your objects by implementing the see compareTo method that relies on the fact that the difference between Total Gym Is The Best Home Exercise Equipment for Your Total Fitness. Endorsed by Christie Brinkley, Chuck Norris, and as seen on TV, Total Gym is the best at-home fitness and gym equipment on the market.

**Core Java: Comparator vs. Comparable,** Internationalization (I18N) - what is the difference between the comparable Comparable This interface imposes a total ordering on the objects of each The natural ordering for a class C is said to be consistent with equals if and only if (e1. Figure 1: A ﬂrst-order thermal model representing the heat exchange between a laboratory vacuum °ask and the environment. Solution: The walls of the °ask may be modeled as a single lumped thermal resistance Rt and a linear graph for the system drawn as in Fig. 1. The environment is assumed to act as a temperature source Tamb(t). The state equation for the system, in terms of

##### Comments

- What exactly do you mean by
**all values can be compared to all other values.**Can you elaborate more on this? - @AbhinavVutukuri in the collection you can say some are comparable and others are not. All values have to be comparable to every other value.
- What exactly do you mean by
**all values can be compared to all other values.**Can you elaborate more on this? - That's what I didn't get. Its like a riddle to me. can u please explain what you mean by default ordering
- lol i know but as i said didn't quite understood it when u said natural cause the meaning is ambiguous to understand for me :(
- When you define object, you can choose ordering which will be used when no other ordering is provided. So one usually implements ordering that is used most often in practice.
- @Ha. But what is
**total ordering**? - @AnV - Suppose you have a class 'Student' then ordering the Student Collection based on multiple/different elements of class 'Student' is called total ordering.