list.sort(): heaviest item?

S

Steven Clark

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?

Looking at http://www.python.org/doc/2.3.5/ref/comparisons.html
"Most other types compare unequal unless they are the same object; the
choice whether one object is considered smaller or larger than another
one is made arbitrarily but consistently within one execution of a
program."

makes me unsure.

It looks like "None" always ends up at the start ("lightest"), but I
want the opposite ("heaviest").

-Steven
 
D

Diez B. Roggisch

Steven said:
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?

Looking at http://www.python.org/doc/2.3.5/ref/comparisons.html
"Most other types compare unequal unless they are the same object; the
choice whether one object is considered smaller or larger than another
one is made arbitrarily but consistently within one execution of a
program."

makes me unsure.

It looks like "None" always ends up at the start ("lightest"), but I
want the opposite ("heaviest").

You can pass a cmp-function that will always make one object being greater
than all others.

Diez
 
S

Steven Clark

You can pass a cmp-function that will always make one object being greater
than all others.

Diez
--

Yeah, I figured it out 2 minutes after I posted, d'oh!

class Anvil(object):
def __cmp__(self. other):
return 1

Sorry for the wasted space.
 
R

Raymond Hettinger

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?

Since the other guys gave you the real answer, how about this:

sentinel = object()
mylist.sort()
mylist.append(sentinel)

_ ~
@ @
\_/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top