sort by last then by first

Padraig at Linux.ie Padraig at Linux.ie
Tue Jan 28 08:56:32 EST 2003


Andrew Dalke wrote:
> Padraig at Linux.ie wrote:
> 
>> wouldn't it be cool if [].sort() took an optional parameter that
>> was essentially the --key option in gnu sort. in this e.g:
>>
>> names.sort("--key=1,1 --key=0,0")
>>
>> or for more complicated forward and then reverse sorting like you
>> suggested:
>>
>> names.sort("--key=1,1r --key=0,0")
> 
> 
> No, it would not.
> 
> 1) requires dispatch based on type (string vs. function)

so?

> 2) would be just about the only function where the argument
> parameters are serialized into a string.  (Are there others
> in the standard library?)

well I was just suggested that so that the sort docs would
suffice and also more users would know the syntax straight off.
You could of course do

> 3) non-obvious (to me at least) on what the different args mean
> (eg, why "--key=1,1" instead of "--key=1"?  I'm guessing it's
> because the key is start/end column, in which case it disagrees
> with Python syntax of start/end+1 column, as in "--key=1,2
> --key=0,1".  Then again, why have the '--key' term, expressing
> it perhaps as "1r,0" or even support range values as "(1,3),5,4"
> or "(1,3),(5,3)" which sorts on rows 1, 2, 5, 4.  And is 1 the
> first column or the second?)

Yes you're probably right that gnu sort has too much baggage.
How about passing a tuple like: ("1(r)","0(n)")
This would first reverse sort on second "column" and
then forward sort on first column treating strings as numeric

> 4) More generic to have a parse function which turns such a
> string into a comparison function, so you have
> 
> names.sort(GnuSortOpt("--key=1,1 --key=0,0"))

Hmm, maybe.

> 5) Doesn't handle the more common case of sorting by
> attribute name.

("attr2(r)", "attr1(n)")

Pádraig.





More information about the Python-list mailing list