62

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 ?

Raedwald
  • 46,613
  • 43
  • 151
  • 237
Vinoth Kumar C M
  • 10,378
  • 28
  • 89
  • 130

5 Answers5

73

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.

Drew Noakes
  • 300,895
  • 165
  • 679
  • 742
krtek
  • 26,334
  • 5
  • 56
  • 84
  • Here is the natural sorting algorithm in javascriptfor more understanding 1. algo:http://js-naturalsort.googlecode.com/svn/trunk/naturalSort.js 2. Unit Test: http://code.google.com/p/js-naturalsort/source/browse/trunk/unit-tests.html – Gaurav Arora Feb 23 '14 at 03:45
  • 1
    Another good description and a smaller implementation: http://blog.codinghorror.com/sorting-for-humans-natural-sort-order/ – dresende Mar 04 '14 at 19:52
  • In Java, the "natural order" isn't one that "seems natural" by some subjective measure, but for objects, objectively the order imposed by the `Comparable#compareTo` method, however it seems to humans. – Lew Bloch Apr 03 '17 at 00:50
7

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

Tafkadasoh
  • 4,667
  • 4
  • 27
  • 31
ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
  • So this is about Strings only ? An object with only age and date does not have natural ordering ? – Vinoth Kumar C M Mar 02 '11 at 13:15
  • 1
    A date is usually sorted as if it's a string 'yyyy-mm-dd'. In this case there is no need for "natural" sorting as it's already done since the small numbers have leading zeros. – ThiefMaster Mar 02 '11 at 13:18
  • In addition, different languages have different sorting rules or collations, so natural order depends. See [Wikipedia](http://en.wikipedia.org/wiki/Collation) – vonPryz Mar 02 '11 at 13:20
2

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...
Giorgi Tsiklauri
  • 9,715
  • 8
  • 45
  • 66
  • This is too simply put. Both Natural and Alphanumeric ordering will order string numbers as "1, 2, 3,..." Where they differ is when you have multiple consecutive digits. Alphanumeric will treat each individual digit as a separate character, while Natural will lump consecutive digits together and treat them as their whole value. Alphanumeric: "1, 10, 100, 1000, 1000000, 2, 20, 200, 2000000" Natural: "1, 2, 10, 20, 100, 200, 100, 100000, 2000000" Also, they have nothing to do with ascending vs. descending. – Erick P Feb 06 '23 at 18:25
1

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.

tonix
  • 6,671
  • 13
  • 75
  • 136
0

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.

dhanu10896
  • 315
  • 2
  • 16