Is the term "natural order" specific to Java?

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

If the list consists of String elements, it will be sorted in Alphabetical order. If it consists of Date elements, it will be sorted in chronological order. How did this happen? String and date implement the Comparable interface. Comparable implementations provide a natural order for a class that allows objects of this class to sort automatically. The following table lists some of the more important Java platform classes that implement Comparable.

Is the term "natural order" specific to Java or language independent? For example, is it possible to talk about "natural ordering" in Ruby?

(Note: I'm not talking about the natural order mentioned in Jeff Atwood's blog post. Sorting for people: natural sorting order )

+5
source share
3 answers

This is not a reference to the type of natural ordering, where the numbers inside the lines are sorted “naturally”, rather than lexicographically by numbers. Java defines the term differently.

Change the emphasis:

Comparable implementation provides a natural order for a class, which allows you to automatically sort the objects of this class.

The word "natural" means that if you implement Comparable , then users of your class can easily sort it without the need for a special comparator. Sorting is natural; it is built in; it's free, no need to think.

Is the term "natural order" specific to Java or language independent?

Yes. No. Both? It is specific to Java, since the documentation in italics naturalOrder this term and the naturalOrder method naturalOrder . However, this concept applies to other languages.

For example, is it possible to talk about "natural ordering" in Ruby?

Could you. If you are talking with a cuff, you can use this term. If you wrote formally, it would be wise to define it. Due to the confusion over Atwood's use of the term, I would prefer something else. Let's say "default order".

+5
source

I believe the term has a specific meaning in Java

At least in the official documentation.

From the API document for the Comparable interface:

This interface imposes a complete order on the objects of each class that implements it. This ordering is called the natural ordering of the class, and the method of comparing classes is called its natural method of comparison.

If we are familiar with this line from the API document. Then we see that the class implements the Comparable interface, and we see that somewhere / someone mentions the "natural order" of two instances of A , we know that we can talk about the order imposed by the compareTo method. The question is whether the person who speaks / writes this term also knows about this particular meaning and uses it in context?

Of course, the API document uses this term in this way because the first thing it does is define it.

0
source

No, the term "natural order" is not specific to Java. My opinion is that it does not apply to programming languages ​​at all; rather, all programming languages ​​(although they may or may not use the term explicitly) rely on the concept.

For example, how does a method like max(a, b) or a.after(b) if the concept of natural ordering does not exist? We know the natural order of integers: 1, 2, 3, ...; dates 1/1/1990, 1/2/1990, 1/1/1991, ...; time: 12:00, 12:01, 1:01 ... These systems are defined by man, but this is what we expect. If we had integers ordered 1, 3, 2, 4, that would be unnatural.

Your quote, as I read it, offers exactly that. A type that implements the Comparable interface provides a “standard”, well-defined, or expected order. For types defined by developers, the developer must provide natural ordering (as Java developers did with Numbers) or define the natural order, as we often do with complex types.

When a class implements the Comparable interface, it provides an orderly (natural) change-only ordering, providing a custom Comparator . However, there is a limited number of objects or systems that we can represent in software that has a true, well-understood and accepted natural order. Many types, such as students, cars, and users, may depend on one or a combination of attributes that determine their order, which may seem natural at all.

0
source

Source: https://habr.com/ru/post/1258500/


All Articles