CODE: Generic Sort Routine

Andrew Henshaw andrew.henshaw at
Mon Aug 23 13:27:42 CEST 2004

Kamilche wrote:

> I've written a generic sort routine that will sort dictionaries,
> lists, or tuples, either by a specified key or by value.
> Comments welcome!

>     if key:
>         sorted = zip([getattr(item, key) for item in thelist],
> thelist)
>     else:
>         sorted = list(thelist)

I'd change that 'if key:' line to 'if key is not None:', so that you could
sort on other non-True values.


More information about the Python-list mailing list