Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: How to impose a numeric sort on negative numbers?



Peter Stephenson wrote on Fri, 03 Jul 2020 20:08 +00:00:
> On Thu, 2020-07-02 at 08:46 -0700, Bart Schaefer wrote:
> > Peter Stephenson wrote on Wed, 01 Jul 2020 20:42 +0100:
> > > > +++ b/Doc/Zsh/expn.yo
> > > > @@ -1097,6 +1097,11 @@ 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(DASH()))(
> > > > +As tt(n), but a leading minus sign indicates a negative decimal
> > > > +integer.  A solo `tt(DASH())' not followed by an integer does
> > > > +not trigger numeric sorting.
> > > > +)
> > 
> > It might be possible to change this.  For numeric sort, the function
> > eltpcmp() in sort.c ignores everything other than digits that compares
> > the same in any pair of the strings.  It could recognize a leading
> > hyphen as special.
> 
> (I've committed it with DASH() turned into -.)
> 

(Turns out there were five instances of tt(DASH()), not two as I implied
in my review, so I've gone ahead and changed those as well.)

> The doc was just to record the fact that I didn't treat a "-" as "-0",
> just as a character "-".  That was a deliberate choice --- I'm
> assuming the usual case of a numeric sort will have at least one
> decimal digit in the part to be sorted and anything else is
> just a hyphen (and definitely not a dash).

FWIW, I thought at first reading that that second sentence referred to
the `tt(-)' in the parameter expansion flags, not to the one in the
input data.  I'd never have expected "-" or "-foo" to be considered an
integer in the first place, just like "" and "foo" aren't considered integers.

Incidentally, should "+42" be sorted as an integer?  Right now it isn't.

Cheers,

Daniel



Messages sorted by: Reverse Date, Date, Thread, Author