Subject: Re: bug#22275: a before null

From: Assaf Gordon <[email-masked]>
Date: Wed, 30 Dec 2015 13:03:03 -0500

tag 22275 notabug
close 22275
stop

Hello Gregory,

On 12/30/2015 10:46 AM, gregory mott wrote:
> in what circumstances is this a sensible and/or desirable result?:
>
> $ sort -V<<eoi
>  > 7.7z
>  > 7a.7z
>  > eoi
> 7a.7z
> 7.7z

GNU sort's "-V" option is modeled after Debian's version sorting algorithm, explained here:
   https://www.debian.org/doc/debian-policy/ch-controlfields.html#s-f-Version
The goal of this algorithm is to sort version of Debian packages, hence it has some particular rules.

First,
see that it is consistent with Debian's sorting rules:

     $ dpkg --compare-versions '7.7z' gt '7a.7z' && echo greater
     greater

That is, '7.7z' is *greater* (=comes after) '7a.7z' - thus 'sort -V' is correct (or at least - consistent with the defined algorithm).

Second,
Examining why is that the case based on the algorithm:
The Debian policy says (paraphrasing):
1. compare the non-digits part
   1.1 letters sorts earlier than any non-letter
   1.2 except tilde which comes before anything else
2. compare the digits part
3. repeat

And so the break-down of the compared parts in your two strings are:
1. "7" vs "7"
2. "." vs "a."
3. "7" vs "7"
4. "z" vs "z"

If I understand correctly, your expectation was that the string is split by dots, hence it is "null" vs "a".
However, the algorithm compares "." vs "a.", and sorts letters earlier than non-letters (see step 2 above. note that steps 3 and 4 are not actually performed,  since the result of step 2 is not equal).

Third,
What is the purpose of such ordering?
That is a question for the Debian Policy writer, however I could hypothesize that they considered versions such as "7a", "7b" to be "7alpha" and "7beta", and so when version "7" is released - it is "newer" (and therefore must sort after the alpha/beta ones).

As such, this is not a bug in gnu sort, and I'm closing the bug,
but comments are welcomed and the discussion can continue by replying to this thread.

regards,
  - assaf

P.S.
For reference, the string-version comparison implementation is in gnulib, in this file:
  http://git.savannah.gnu.org/cgit/gnulib.git/tree/lib/filevercmp.c