2
$\begingroup$

It is also called Natural sort order.

"It's hard to find information on natural sorting"-Jeff Atwood on Coding Horror.

And it seems true today as the Wikipedia entry seems pretty bare.

Definition 1: Non-numeric word-character substrings are sorted lexically, case-insensitively. Numeric substrings are sorted numerically. non words-characters, non-digits are ignored.

Definition 2: Natural sort order is an ordering of strings in alphabetical order, except that multi-digit numbers are ordered as a single character.

The main difference is that Definition 1 allows decimals while Definition 2 does not. For example I have the list ["t8.1", "t8.11", "t8.2", "t7.2", "t7.11"].

Definition 1 would yeild: ["t7.11", "t7.2", "t8.1", "t8.11", "t8.2"].

Definition 2 would yeild: ["t7.2", "t7.11", "t8.1", "t8.2", "t8.11"].

Python's natsorted function was definition 1 until v4.0.0 and now uses realsorted for this behavior. Python's natsorted function currently follows definition 2.

Perl follows definition 1 (which is where I found it.

Php follows definition 2

MATLAB uses definition 2 for non-version numbers.

Dave Koelle implements definition 1 for his examples but says it's a 'glitch' and thus supporting definition 2 as correct. In most the linked code, the code treats '.' as a string but what if it was a decimal in a numeric substring? Dave Koelle notes this as a glitch by saying "There is currently a glitch when it comes to periods/decimal points - specifically, periods are treated only as strings, not as decimal points.".

To me, the word numeric would imply decimals but in practice it seems that this is not the case unless specifically noted to include decimals.

What does Alphanumberic/Natural Order Sort mean exactly without additional qualifiers? For example: In "Natural Order Sort with decimals", "with decimals" would be an additional qualifiers. Which definition is correct for "Natural Order Sort"?

Alphanumberic (Non-Decimal) vs Alphanumberic (Decimal)

$\endgroup$
  • 4
    $\begingroup$ Er, um... blogs don't tend to write themselves. Why not give credit to the author instead of the blog? $\endgroup$ – jpaugh Jul 12 '16 at 16:16
  • $\begingroup$ I would like to add.. what if there are special character in the name too like #1.txt. So if we ask sort alphanumerically will they count too. $\endgroup$ – Denis Jul 12 '16 at 21:25
  • 1
    $\begingroup$ Are you asking on a computer science site how programmers call things? $\endgroup$ – Raphael Jul 12 '16 at 21:25
  • 1
    $\begingroup$ It is "alphanumeric" without "b". I see the ambiguous problem. There are more definitions, but please read it more carefully. Natural sort is used exactly as I described definition. The parts where extended decimals with sign are used is called "natural real sort", it takes floating point numbers from string. (Real here is also bad definition, since the floating points with fixed length are used). Per language extension gives for example case-insensitive comparisons. It is not shown in examples, but what happens with different notations? 1e299, 0x34 or 0777? So "scientific natural order"? $\endgroup$ – Evil Jul 13 '16 at 21:47
  • 1
    $\begingroup$ So straight from definition without examples or language specific implementations the natural order was like in the blog created to handle non-zero-padded integers (just like in the blog example). This is recognized definition. The other definitions are different types of sort with less known names, or not recognized by name (but recognized by "in language X compiler/interpreter version Y+" works like this). The glitch is in fact according to definition proper - "." is char. The other names are not well recognized (easily checked by Google toss). $\endgroup$ – Evil Jul 13 '16 at 21:53
3
$\begingroup$

Alphanumeric sort means that every character is compared according to ascii representation.
Natural sort treats consecutive digits as one chatacter, so actually compares as integer. There is no place for decimals, but when you compare two numbers in the form $X.Y > W.Z$ it looks like decimal compare.

So what does "natural sort does not compare decimals" really mean?
The example above is just coincidence, like reading month.day as float and sorting will give good order, but adding and subtracting is not defined. Same here, "-" will not be identified as part of decimal, so comparison will be shifted. The separator might be given as anything, but will change results when inconsistent.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.