Gnu/coreutils/Version 002dsort-ordering-rules
From Get docs
30.2.1 Version-sort ordering rules
The version sort ordering rules are:
- The strings are compared from left to right.
- First the initial part of each string consisting entirely of non-digit characters is determined.
- These two parts (one of which may be empty) are compared lexically. If a difference is found it is returned.
- The lexical comparison is a comparison of ASCII values modified so that:
- all the letters sort earlier than all the non-letters and
- so that a tilde sorts before anything, even the end of a part.
- Then the initial part of the remainder of each string which consists entirely of digit characters is determined. The numerical values of these two parts are compared, and any difference found is returned as the result of the comparison.
- For these purposes an empty string (which can only occur at the end of one or both version strings being compared) counts as zero.
- These two steps (comparing and removing initial non-digit strings and initial digit strings) are repeated until a difference is found or both strings are exhausted.
Consider the version-sort comparison of two file names:
foo07.7z
and foo7a.7z
. The two strings will be broken
down to the following parts, and the parts compared respectively from
each string:
foo vs foo (rule 2, non-digits characters) 07 vs 7 (rule 3, digits characters) . vs a. (rule 2) 7 vs 7 (rule 3) z vs z (rule 2)
Comparison flow based on above algorithm:
- The first parts (
foo
) are identical in both strings. - The second parts (
07
and7
) are compared numerically, and are identical. - The third parts (‘
.
’ vs ‘a.
’) are compared lexically by ASCII value (rule 2.2). - The first character of the first string (‘
.
’) is compared to the first character of the second string (‘a
’). - Rule 2.2.1 dictates that "all letters sorts earlier than all non-letters". Hence, ‘
a
’ comes before ‘.
’. - The returned result is that
foo7a.7z
comes beforefoo07.7z
.
Result when using sort:
$ cat input3 foo07.7z foo7a.7z $ sort -V input3 foo7a.7z foo07.7z
See Differences from the official Debian Algorithm for additional rules that extend the Debian algorithm in coreutils.