Sorting a list with entries of unequal types
Ben Finney
ben+python at benfinney.id.au
Thu Jan 28 20:13:50 EST 2010
Paul Rubin <no.email at nospam.invalid> writes:
> 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?
The same ordering I'd get in Python 2; that is, determined by the types
of the elements.
Peter Otten <__peter__ at web.de> writes:
> I can't find the relevant part of the 2.6 documentation, but something like
>
> >>> def key(x):
> ... t = type(x)
> ... t = compat.get(t, t)
> ... return t.__name__, id(t), x
> ...
> >>> compat = {bool: float, int: float}
The problem with this approach is that it assumes I know all the types
of elements that will be sorted; I don't. I want to sort a list coming
from some other part of the code, and I don't want to arbitrarily limit
the types of the list elements.
Essentially, I want the same behaviour I'd get from Python 2's sort: the
order of the elements is determined by the ordering built into the types
of the elements themselves, with sensible defaults.
--
\ “I distrust those people who know so well what God wants them |
`\ to do to their fellows, because it always coincides with their |
_o__) own desires.” —Susan Brownell Anthony, 1896 |
Ben Finney
More information about the Python-list
mailing list