-1

When we use the Comparable interface to define a natural order of objects, what does natural order even mean?

And after I have implemented the Comparable interface, how can I make use of the objects that have been ordered? How is the interface used and for what purpose?

Zabuzard
  • 25,064
  • 8
  • 58
  • 82
a_ssis
  • 7
  • 1
  • 4
    Does this answer your question? [what is natural ordering when we talk about sorting?](https://stackoverflow.com/questions/5167928/what-is-natural-ordering-when-we-talk-about-sorting) – Druckles Oct 09 '19 at 08:20
  • When the documentation of a sorting method says the objects will be sorted according to natural ordering, it means they will be sorted according to whatever you choose the `compareTo` method (implementing the Comparable interface) to mean. – DodgyCodeException Oct 09 '19 at 08:25
  • In my opinion, the linked thread is not a duplicate. It only explains 50% of the question, namely what natural ordering means. But not how `Comparable` works, is implemented, used and what purpose it fulfills. – Zabuzard Oct 09 '19 at 09:11

3 Answers3

4

When you implement the interface yourself, the "natural order" is simply what you implemented in the compareTo() method. Then when sorting a collection of comparable objects this ordering would be the default sort order.

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

from Comparable docs

kossmoboleat
  • 1,841
  • 1
  • 19
  • 36
0

The compareTo method of the Comparable interface can be used to sort Lists and arrays of objects that are instances of a class that implements this interface. This ordering is called the natural ordering of that class.

"Lists (and arrays) of objects that implement the Comparable 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 as elements in a sorted set, without the need to specify a comparator."

from: https://docs.oracle.com/en/java/javase/12/docs/api/java.base/java/lang/Comparable.html

0

Explanation

You have to define this order by implementing the compareTo method that is forced on you when implementing Comparable.

This order should be what a human would call natural for this type. So for example ascending by value for integers (1, 2, 5, 9), maybe by age for Persons. It is your choice as designer to choose something that users would find natural and not confusing for your custom classes.

It will then be used as default ordering if you, for example sort a collection of this type.

See the documentation of Comparable and its Comparable#compareTo method:

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 as elements in a sorted set, without the need to specify a comparator.


Example

Here is a small example:

public class Person implements Comparable<Person> {
    private final String name;
    private final int age;

    // Constructor, getters, ...

    @Override
    public int compareTo(Person other) {
        // Compare based on age, ascending
        return Integer.compareTo(age, other.age);
    }
}

With a usage like:

Person first = new Person("John", 20);
Person second = new Person("Jane", 18);
Person third = new Person("Bob", 30);

List<Person> persons = new ArrayList<>();
persons.add(first);
persons.add(second);
persons.add(third);

// Ordering is now: John, Jane, Bob

// Sort based on natural ordering
Collections.sort(persons);

// Ordering is now: Jane, John, Bob
Zabuzard
  • 25,064
  • 8
  • 58
  • 82