Replacing cmp with key for sorting
bearophileHUGS at lycos.com
bearophileHUGS at lycos.com
Mon Nov 3 13:46:24 EST 2008
Alan G Isaac:
> Probably not what you had in mind ...
> ...
> >>> maxlen = max(len(si) for si in s)
> >>> def k(si): return si+'z'*(maxlen-len(si))
This looks a little better:
assert isinstance(s, str)
sorted(s, key=lambda p: p.ljust(maxlen, "\255"))
If the string is an unicode that may not work anymore.
I don't know if there are better solutions.
Bye,
bearophile
More information about the Python-list
mailing list