list.sort(): heaviest item?
Jeffrey Froman
jeffrey at fro.man
Tue Apr 8 11:49:26 EDT 2008
Steven Clark wrote:
> If I have a list of items of mixed type, can I put something into it
> such that after a list.sort(), is guaranteed to be at the end of the
> list?
<snip doc excerpt>
> It looks like "None" always ends up at the start ("lightest"), but I
> want the opposite ("heaviest").
I don't know of an object in the standard library that is guaranteed to
be "heaviest", but it is trivial to create one:
>>> class Heaviest(object):
... def __cmp__(self, other):
... return 1
...
>>> Heaviest = Heaviest()
>>> L = [tuple(), list(), dict(), Heaviest, str(), int(), None]
>>> L.sort()
>>> L
[None, 0, {}, [], '', (), <__main__.Heaviest object at 0xb7c04a8c>]
Ordering with respect to another object that defines a similar __cmp__
function will still be arbitrary, but maybe you don't have to deal with any
such objects ;-)
Jeffrey
More information about the Python-list
mailing list