CODE: Generic Sort Routine

Andrew Henshaw andrew.henshaw at gtri.gatech.edu
Mon Aug 23 07:27:42 EDT 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!

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

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

Andy



More information about the Python-list mailing list