Question about sorted in Python 3.0rc1

Peter Otten __peter__ at web.de
Mon Sep 22 07:18:24 EDT 2008


josh logan wrote:

> A better example would be sorting by increasing last name and
> decreasing first name. This would be easy with the sort function
> comparator, but I can't see how to do the same with the key argument.
> Is the only solution to decorate the Player objects in another class
> that has the appropriate __cmp__ function (or whatever is needed) and
> then retrieve the Player objects back?

Python's sort algorithm is guaranteed to be stable; therefore you can sort
twice:

ordered = sorted(players, key=lambda p: p.fname, reverse=True)
ordered.sort(key=lambda p: p.lname)

Peter



More information about the Python-list mailing list