what is natural ordering when we talk about sorting?

SortingLanguage Agnostic

Sorting Problem Overview


What is meant by natural ordering . Suppose I have an Employee object with name , age and date of joining , sorting by what is natural ordering ?

Sorting Solutions


Solution 1 - Sorting

Natural ordering is a kind of alphanumerical sort which seems natural to humans.

In a classical alphanumerical sort we will have something like :

1 10 11 12 2 20 21 3 4 5 6 7

If you're using Natural ordering, it will be :

1 2 3 4 5 6 7 10 11 12 20 21

Depending on the language, natural ordering sometimes ignore Capital letters and accentuated one (ie all accentuated letters are treated like their non-accentuated counterpart).

Many languages have a function to order a String naturally. However, an Employee is too "high level" for the language, you must decide what it means for you to order them naturally and create the according function.

In my point of view, ordering Employee will start by ordering them by name using a natural sort, then age and finally date of joining.

According to statistics there are two types of categorical variables. Variables having categories without a numerical ordering (nominal) and those which do have ordered categories (ordinal). The example of an Employee's name, age and date of joining is actually considered a nominal variable so there can be no sorting by natural ordering. Natural ordering could exist for example in age had you categorized it in levels of child, teenager, adult, in which one can observe an ascending type of sorting.

Solution 2 - Sorting

For strings containing numbers it means 1,2,3,4,5,6,7,8,9,10,11 instead of 1,10,11,2,3,4,5,6,7,8,9

Solution 3 - Sorting

If someone like me found himself reading the following article:

https://www.copterlabs.com/natural-sorting-in-mysql/

(which by the way is really useful), beware it because that's another method of sorting.

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. Though the article I linked is interesting, don't make the mistake I made and think that that's the correct way to sort naturally.

Solution 4 - Sorting

For Java, The ordering provided by the Comparable interface is called the natural ordering, so the Comparator interface provides, so to speak, an unnatural ordering.

Solution 5 - Sorting

Quite an old question, but very simply put, the Natural Order is an ascending order of the enumerable collection of the comparable elements:

  • For the numbers: 1, 2, 3...
  • For the characters: A, B, C...

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionVinoth Kumar C MView Question on Stackoverflow
Solution 1 - SortingkrtekView Answer on Stackoverflow
Solution 2 - SortingThiefMasterView Answer on Stackoverflow
Solution 3 - SortingtonixView Answer on Stackoverflow
Solution 4 - Sortingdhanu10896View Answer on Stackoverflow
Solution 5 - SortingGiorgi TsiklauriView Answer on Stackoverflow