Sorting using lambda not working in Py2.1?

Brian Quinlan BrianQ at ActiveState.com
Thu May 3 12:58:04 EDT 2001


> I may be doing something wrong, or is this a bug in 2.1?

You are doing something wrong :-) From the docs:

The sort() method takes an optional argument specifying a comparison
function of two arguments (list items) which should return -1, 0 or 1
depending on whether the first argument is considered smaller than, equal
to, or larger than the second argument. Note that this slows the sorting
process down considerably; e.g. to sort a list in reverse order it is much
faster to use calls to the methods sort() and reverse() than to use the
built-in function sort() with a comparison function that reverses the
ordering of the elements.

> 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...
>
> - Tushar.





More information about the Python-list mailing list