Inconsistency with sort -n?
Eric Blake
ebb9@byu.net
Wed Jan 7 14:27:00 GMT 2009
According to Buchbinder, Barry (NIH/NIAID) [E] on 12/31/2008 2:29 PM:
[sorry for my delay in replying]
> `sort -n' and `sort -g' work inconsistently with 0 and -0 if there are leading spaces. Sometimes -0 is before 0, as I would expect, and sometimes it is afterwards. Adding `-b' does not seem to help.
>
> Is this where I should report it or should I go upstream?
If it were a bug, it would be an upstream issue (I reproduced your test
cases on Linux). But it is not a bug; sort is behaving as documented.
> $ echo 0 -1 -0 1 | tr ' ' '\n' | sed -e '/^.$/s/^/ /' | sort -n
> -1
> 0
> -0
> 1
sort -n sorts the entire line based on numeric value (0 and -0 have the
same value), then breaks ties based on byte-wise values (' ' comes before
'-').
> $ echo 0 -1 -0 1 | tr ' ' '\n' | sed -e '/^.$/s/^/ /' | sort -g
> -1
> 0
> -0
> 1
sort -g is slower than sort -n, because it converts to floating point; and
although -0.0 and +0.0 are distinct bit patterns, they still sort equal,
so you ware once again back to the fallback of bytewise comparison to
break ties (and ' ' still comes before '-').
Use sort -u to see that 0 and -0 sort numerically equal, and thus why a
fallback sort must be attempted.
$ echo 0 -1 -0 1 | tr ' ' '\n' | sed -e '/^.$/s/^/ /' | sort -nu
-1
0
1
Or, go one better - use two sort keys. Make the primary key sort
numerically, and the second sort key break ties in favor of '-':
$ echo 0 -1 -0 1 | tr ' ' '\n' | sed -e '/^.$/s/^/ /' | sort -k1,1n -k1r
-1
-0
0
1
--
Don't work too hard, make some time for fun as well!
Eric Blake ebb9@byu.net
volunteer cygwin coreutils maintainer
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 319 bytes
Desc: OpenPGP digital signature
URL: <http://cygwin.com/pipermail/cygwin/attachments/20090107/e371bb62/attachment.sig>
More information about the Cygwin
mailing list