coreutils: Version-sort ordering rules
30.2.1 Version-sort ordering rules
----------------------------------
The version sort ordering rules are:
1. The strings are compared from left to right.
2. First the initial part of each string consisting entirely of
non-digit characters is determined.
1. These two parts (one of which may be empty) are compared
lexically. If a difference is found it is returned.
2. The lexical comparison is a comparison of ASCII values
modified so that:
1. all the letters sort earlier than all the non-letters and
2. so that a tilde sorts before anything, even the end of a
part.
3. 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.
1. For these purposes an empty string (which can only occur at
the end of one or both version strings being compared) counts
as zero.
4. 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:
1. The first parts (‘foo’) are identical in both strings.
2. The second parts (‘07’ and ‘7’) are compared numerically, and are
identical.
3. The third parts (‘‘.’’ vs ‘‘a.’’) are compared lexically by ASCII
value (rule 2.2).
4. The first character of the first string (‘‘.’’) is compared to the
first character of the second string (‘‘a’’).
5. Rule 2.2.1 dictates that "all letters sorts earlier than all
non-letters". Hence, ‘‘a’’ comes before ‘‘.’’.
6. The returned result is that ‘foo7a.7z’ comes before ‘foo07.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.