Comparisons of incompatible types
Tim Chase
python.list at tim.thechases.com
Wed Dec 8 07:29:57 EST 2010
On 12/08/2010 03:47 AM, Steven D'Aprano wrote:
> Or a list that needs to be presented to a human reader in some arbitrary
> but consistent order.
>
> Or a doctest that needs to show the keys in a dict:
>
> >>> d = myfunction()
> >>> sorted(d.keys())
> ['ham', 'spam', 42, None]
>
[snip]
>
> Agreed, but in hindsight I think it would be better if there was a
> separate lexicographic sort function, that guaranteed to sort anything
> (including such unorderable values as complex numbers!), without relying
> on the vagaries of the standard comparison operators.
wouldn't that be something like
sorted(mixedstuff, key=str)
or if all you need is a stable order regardless of what that
order is, one could even get away with:
sorted(mixedstuff, key=id)
-tkc
More information about the Python-list
mailing list