sorting a list
Thorsten Kampe
thorsten at thorstenkampe.de
Thu Apr 29 04:29:58 EDT 2004
[Snip fullquote]
* Sean Berry (2004-04-29 02:50 +0100)
>>>> a = ['A', 'D', 'F', 'a', 'f']
>>>> a.sort(lambda x, y: cmp(string.lower(x), string.lower(y)))
>>>> a
> ['a', 'A', 'D', 'f', 'F']
>
> this will work too, if lower can come before upper.
Yes indeed. But the main point of my posting was to use a general
function sort so you don't have to reinvent the wheel everytime for
every single sorting problem.
Another advantage of my approach is to use a function of *one*
variable (at the top level) and the other ones use a function of two
variables. It doesn't make much difference with this trivial task, but
when the function you want to sort by is much more complex it makes a
hell of a difference.
funcsort(foo, lambda x: x.lower())
is clean code and tells even without a comment that you want to sort
case insensitively.
Thorsten
More information about the Python-list
mailing list