zsh-workers
 help / color / mirror / code / Atom feed
* reverse numeric sorting does not work
@ 2007-07-06 12:24 Vincent Lefevre
  2007-07-06 13:04 ` Peter Stephenson
  0 siblings, 1 reply; 2+ messages in thread
From: Vincent Lefevre @ 2007-07-06 12:24 UTC (permalink / raw)
  To: zsh-workers

Hi,

Reverse numeric sorting doesn't seem to work:

prunille% echo $ZSH_VERSION
4.3.4
prunille% foo=(a6 a117 a17 b6 b117 b17)
prunille% echo ${(n)foo}
a6 a17 a117 b6 b17 b117
prunille% echo ${(On)foo}
b6 b17 b117 a6 a17 a117

instead of

b117 b17 b6 a117 a17 a6

Also the man page is ambiguous:

Are the numbers (nonnegative) integer or floating-point numbers?

It also says: "if the first differing characters of two test strings
are not digits, sorting is lexical." then "Trailing non-digits are
not sorted; the order of `2foo' and `2bar' is not defined." But on
this example, the first differing characters are not digits, so that
the sorting should be lexical, hence defined!

-- 
Vincent Lefèvre <vincent@vinc17.org> - Web: <http://www.vinc17.org/>
100% accessible validated (X)HTML - Blog: <http://www.vinc17.org/blog/>
Work: CR INRIA - computer arithmetic / Arenaire project (LIP, ENS-Lyon)


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: reverse numeric sorting does not work
  2007-07-06 12:24 reverse numeric sorting does not work Vincent Lefevre
@ 2007-07-06 13:04 ` Peter Stephenson
  0 siblings, 0 replies; 2+ messages in thread
From: Peter Stephenson @ 2007-07-06 13:04 UTC (permalink / raw)
  To: zsh-workers

On Fri, 6 Jul 2007 14:24:15 +0200
Vincent Lefevre <vincent@vinc17.org> wrote:
> Reverse numeric sorting doesn't seem to work:

Yes: failure in test strategy (there isn't a test).

> Also the man page is ambiguous:
> 
> Are the numbers (nonnegative) integer or floating-point numbers?

Yes, 3.3 is also a "decimal number" but isn't sorted after the ".".

> It also says: "if the first differing characters of two test strings
> are not digits, sorting is lexical." then "Trailing non-digits are
> not sorted; the order of `2foo' and `2bar' is not defined." But on
> this example, the first differing characters are not digits, so that
> the sorting should be lexical, hence defined!

Given that 02 and 2 are defined to sort in that order (I've been more picky
about the wording with leading zeroes to cover all bases), that doesn't
seem to leave anything for that second sentence to cover: either the first
differing part is an integer, in which case there's a defined order even if
two different sets of digits are numerically equal (proof by grabbing a
first year maths student), or the first different part is not numerical, in
which case sorting is lexical.  So I've omitted it.

Index: Doc/Zsh/expn.yo
===================================================================
RCS file: /cvsroot/zsh/zsh/Doc/Zsh/expn.yo,v
retrieving revision 1.78
diff -u -r1.78 expn.yo
--- Doc/Zsh/expn.yo	30 May 2007 03:08:22 -0000	1.78
+++ Doc/Zsh/expn.yo	6 Jul 2007 12:59:41 -0000
@@ -764,13 +764,12 @@
 Convert all letters in the result to lower case.
 )
 item(tt(n))(
-Sort decimal numbers numerically; if the first differing
+Sort decimal integers numerically; if the first differing
 characters of two test strings are not digits, sorting
-is lexical.   Numbers with initial zeroes
-are sorted before those without.  Hence the array `tt(foo1 foo02
-foo2 foo3 foo20 foo23)' is sorted into the order shown.  Trailing
-non-digits are not sorted; the order of `tt(2foo)' and `tt(2bar)'
-is not defined.  May be combined with `tt(i)' or `tt(O)'.
+is lexical.   Integers with more initial zeroes
+are sorted before those with fewer or none.  Hence the array `tt(foo1 foo02
+foo2 foo3 foo20 foo23)' is sorted into the order shown.
+May be combined with `tt(i)' or `tt(O)'.
 )
 item(tt(o))(
 Sort the resulting words in ascending order; if this appears on its
Index: Src/sort.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/sort.c,v
retrieving revision 1.6
diff -u -r1.6 sort.c
--- Src/sort.c	13 May 2007 20:22:02 -0000	1.6
+++ Src/sort.c	6 Jul 2007 12:59:43 -0000
@@ -134,9 +134,9 @@
 		    while (idigit(*as) && idigit(*bs))
 			as++, bs++;
 		    if (idigit(*as) && !idigit(*bs))
-			return 1;
+			return sortdir;
 		    if (idigit(*bs) && !idigit(*as))
-			return -1;
+			return -sortdir;
 		}
 	    }
 	}
Index: Test/D04parameter.ztst
===================================================================
RCS file: /cvsroot/zsh/zsh/Test/D04parameter.ztst,v
retrieving revision 1.25
diff -u -r1.25 D04parameter.ztst
--- Test/D04parameter.ztst	13 Apr 2007 11:54:17 -0000	1.25
+++ Test/D04parameter.ztst	6 Jul 2007 12:59:43 -0000
@@ -913,3 +913,10 @@
 >AXB C1D
 >AB C0D
 >AB C0D
+
+  foo=(a6 a117 a17 b6 b117 b17)
+  print ${(n)foo}
+  print ${(On)foo}
+0:Numeric sorting
+>a6 a17 a117 b6 b17 b117
+>b117 b17 b6 a117 a17 a6

-- 
Peter Stephenson <pws@csr.com>                  Software Engineer
CSR PLC, Churchill House, Cambridge Business Park, Cowley Road
Cambridge, CB4 0WZ, UK                          Tel: +44 (0)1223 692070


To access the latest news from CSR copy this link into a web browser:  http://www.csr.com/email_sig.php

To get further information regarding CSR, please visit our Investor Relations page at http://ir.csr.com/csr/about/overview


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2007-07-06 13:04 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-07-06 12:24 reverse numeric sorting does not work Vincent Lefevre
2007-07-06 13:04 ` Peter Stephenson

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

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 NNTP newsgroup(s).