Bug 213128 - version sort (sort -V) order incorrect with non-alphanumeric characters
Summary: version sort (sort -V) order incorrect with non-alphanumeric characters
Status: New
Alias: None
Product: Base System
Classification: Unclassified
Component: bin (show other bugs)
Version: 10.3-RELEASE
Hardware: i386 Any
: --- Affects Some People
Assignee: freebsd-bugs (Nobody)
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2016-09-30 18:58 UTC by Serge
Modified: 2016-10-01 01:15 UTC (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Serge 2016-09-30 18:58:28 UTC
On FreeBSD 10.3-RELEASE-p9 with /usr/bin/sort version 2.3-FreeBSD:

Version-sorting produces incorrect order if non-alphanumeric characters are present in sorted lines. Prefix (name) parts of the sorted lines sharing the same starting characters are seemingly sorted in the reverse order. This is an example from my package folder:

$ ls /usr/ports/packages/All | sort -V
…
pkgconf-0.9.12_1.txz
pkgconf-1.0.1.txz
pkg-1.6.4_1.txz
pkg-1.7.1.txz
pkg-1.7.2.txz
…

To reproduce the behaviour, the following can be done. The next two commands produce identical and correct output:

$ echo -e "aa\nab\nabcdefg\nabd\nabcde\nabc\nabcdef"|sort
aa
ab
abc
abcde
abcdef
abcdefg
abd

And

$ echo -e "aa\nab\nabcdefg\nabd\nabcde\nabc\nabcdef"|sort -V
aa
ab
abc
abcde
abcdef
abcdefg
abd

However, the second of these two produces clearly incorrect sort order:

$ echo -e "aa-\nab-\nabcdefg-\nabd-\nabcde-\nabc-\nabcdef-“|sort
aa-
ab-
abc-
abcde-
abcdef-
abcdefg-
abd-

But

$ echo -e "ab-\nabcdefg-\nabd-\nabcde-\nabc-\nabcdef-"|sort -V
abcdefg-
abcdef-
abcde-
abc-
abd-
ab-

The same behaviour is observed when different non-alphanumeric characters are appended to the end of the sorted strings:

echo -e "ab-\nabcdefg&\nabd#\nabcde@\nabc&\nabcdef_"|sort -V
abcdefg&
abcdef_
abcde@
abc&
abd#
ab-
Comment 1 Serge 2016-09-30 19:05:11 UTC
I don't know whether or not the reported behaviour is related to another peculiar case of version sort. This is an example from my packages folder:

$ ls /usr/ports/packages/All | sort -V
…
openssl-1.0.2i,1.txz
openssl-1.0.2j,1.txz
openssl-1.0.2_13.txz
openssl-1.0.2_14.txz
openssl-1.0.2_15,1.txz
…

Both alphabetically and by version sequence, 'openssl-1.0.2j,1.txz' should follow 'openssl-1.0.2_15,1.txz', but the resulting order is not correct.