Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is the term "natural ordering" specific to Java?

The tutorial Object Ordering refers to the concept of "natural ordering":

If the List consists of String elements, it will be sorted into alphabetical order. If it consists of Date elements, it will be sorted into chronological order. How does this happen? String and Date both implement the Comparable interface. Comparable implementations provide a natural ordering for a class, which allows objects of that class to be sorted automatically. The following table summarizes some of the more important Java platform classes that implement Comparable.

Is the term "natural ordering" specific to Java, or language-independent? For example, could I talk about "natural ordering" in Ruby?

(Note: I'm not talking about Natural sort order, mentioned in Jeff Atwood's blog post Sorting for Humans : Natural Sort Order)

like image 261
Andrew Grimm Avatar asked Oct 19 '16 23:10

Andrew Grimm


People also ask

What is natural ordering in Java?

Natural ordering is the default ordering of objects of a specific type when they are sorted in an array or a collection. The Java language provides the Comparable interface that allows us define the natural ordering of a class.

How does Java know that a type has a natural ordering?

Natural order is one judging by which, taking any 2 objects of same type, you can say, which object is bigger by value and which is smaller, or they are equal. In Java, if class T implements Comparable<T> , it is called having natural order.

What is natural order in programming?

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.

Which interface is associated with natural ordering?

The Comparable and Comparator Interfaces. The Comparable interface imposes a total ordering on the implemented class. This total ordering is referred to as the natural ordering. The comparison for ordering is asserted by the compareTo method of this interface, called the natural ordering method.


1 Answers

This is not a reference to the type of natural ordering where numbers inside of strings are sorted "naturally" instead of lexicographically digit-by-digit. Java defines the term differently.

Let's change the emphasis:

Comparable implementations provide a natural ordering for a class, which allows objects of that class to be sorted automatically.

The word "natural" means that if you implement Comparable then users of your class can sort it easily without needing a custom comparator. The sorting is natural; it's built in; it's free, no thinking required.

Is the term "natural ordering" specific to Java, or language-independent?

Yes. No. Both? It's specific to Java insofar as the documentation italicizes the term and there is a naturalOrder method. The concept is applicable to other languages, though, sure.

For example, could I talk about "natural ordering" in Ruby?

You could. If you were talking off the cuff you could use the term. If you were writing formally it would be prudent to define it. Because of the confusion with Atwood's use of the term, I'd prefer a different one. Say, "default ordering".

like image 100
John Kugelman Avatar answered Sep 29 '22 14:09

John Kugelman