Sorting a list with entries of unequal types

MRAB python at mrabarnett.plus.com
Fri Jan 29 01:58:34 CET 2010


Paul Rubin wrote:
> Ben Finney <ben+python at benfinney.id.au> writes:
>> So how should I be sorting a list with entries of “unequal types” such
>> that it will work in Python 3?
> 
> Um, what ordering do you want?  Basically you have to write a custom key
> function (they removed the option of writing a comparison function).
> 
> Maybe something like
> 
>     foo.sort(key=lambda x: (type(x), x))
> 
> would be enough to fool the sorting function?  I don't have python 3
> so I haven't tried it.

You can't compare types:

TypeError: unorderable types: type() < type()

but you can compare their names.



More information about the Python-list mailing list