CODE: Generic Sort Routine

Andrew Henshaw andrew.henshaw at gtri.gatech.edu
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!

...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