Replacing cmp with key for sorting

bearophileHUGS at lycos.com bearophileHUGS at lycos.com
Mon Nov 3 13:51:40 EST 2008


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)

Bye,
bearophile



More information about the Python-list mailing list