Sorting using lambda not working in Py2.1?

Alex Martelli aleaxit at yahoo.com
Thu May 3 12:57:51 EDT 2001


"mary" <mary.stern at virgin.net> wrote in message
news:9crvf1$3588$1 at sp15at20.hursley.ibm.com...
> I may be doing something wrong, or is this a bug in 2.1?
>
> x = (9,5,4,3,1,2)
>
> print x.sort()
> -> (1,2,3,4,5,9)
> print x.sort(lamda x,y: x<y)
> -> (9,5,4,3,1,2) !!
>
> I'm using the 2.1 released source, built on Redhat 6.2...

I assume you're not quoting directly (tuples can't be
sorted, .sort returns None, lambda is misspelled), but
the issue seems to be that you're using
    lambda x, y: x<y
as the argument to sort.  This violates the semantic
constraints on sort's optional argument: it must be
a function that only returns 0 when arguments are to
be considered EQUAL for sorting purposes.  This will
also return 0 when x>y -- not just when they're equal.


Alex






More information about the Python-list mailing list