Learning Python via a little word frequency program
fredrik at pythonware.com
Wed Jan 9 13:14:26 CET 2008
Andrew Savige wrote:
> Neat. Is there any way to use sorted() with multiple sort keys? ...
sure! just return the "composite key" as a tuple:
# sort on descending count, ascending name
deco = sorted(freq.items(), key=lambda x: (-x, x))
(when comparing tuples, Python first compares the first item from each
tuple. if they're equal, it continues with the second item, and so on).
More information about the Python-list