public inbox for cygwin@cygwin.com
 help / color / mirror / Atom feed
From: Eric Blake <ebb9@byu.net>
To: cygwin@cygwin.com
Subject: Re: Inconsistency with sort -n?
Date: Wed, 07 Jan 2009 14:27:00 -0000	[thread overview]
Message-ID: <4964AA21.4040900@byu.net> (raw)
In-Reply-To: <0105D5C1E0353146B1B222348B0411A211B41DEC@NIHMLBX02.nih.gov>

[-- Attachment #1: Type: text/plain, Size: 1694 bytes --]

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


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 319 bytes --]

      parent reply	other threads:[~2009-01-07 13:12 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-12-31 21:39 Buchbinder, Barry (NIH/NIAID) [E]
2009-01-02 23:07 ` Dave Korn
2009-01-07 14:27 ` Eric Blake [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4964AA21.4040900@byu.net \
    --to=ebb9@byu.net \
    --cc=cygwin@cygwin.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).