Replacing cmp with key for sorting

Arnaud Delobelle arnodel at googlemail.com
Mon Nov 3 19:57:59 CET 2008


bearophileHUGS at lycos.com writes:

> Arnaud Delobelle:
>> Here's another idea, probably more practical:
>> >>> sorted(s, key=lambda x: tuple(256-ord(l) for l in x), reverse=True)
>
> Nice.
> A variant that probably works with unicode strings too:
>
> print sorted(s, key=lambda x: [-ord(l) for l in x], reverse=True)

Of course that's better! (although mine will work with unicode if yours
does).  It's funny how the obvious escapes me so often.  Still I think
the idea of the 'double reverse' (one letterwise, the other listwise)
was quite good.

-- 
Arnaud



More information about the Python-list mailing list