sort descending on a[1], ascending on a[0]

Delaney, Timothy tdelaney at avaya.com
Thu Oct 10 19:54:06 EDT 2002


> From: Manuel M. Garcia [mailto:mgarcia at cole-switches.com]
> 
> What is everyones favorite way of sorting descending on a[1], and
> ascending on a[0]? (or however you wish, even changing this on
> the fly)
> 
> I am not a big fan of passing sort() a comparison function, but I
> don't see how I could use the Decorate - Sort - Undecorate (DSU)
> technique.

Probably the easiest way would be to convert the strings to their ordinal
values, and for the ascending sort, then negate each ordinal value.

But unless you are having performance problems, the absolute easiest way in
this case is to use a comparison function. That's what they're there for.

DSU is a wonderful thing, but it is not always the best option. Just most of
the time.

Tim Delaney




More information about the Python-list mailing list