Sort

Greg Landrum glandrum at my-deja.com
Wed Dec 6 09:30:42 EST 2000


In article <oa8s2tsls5foll62r8fpa6nlcpisb6v1ek at 4ax.com>,
  Steve Horne <sh at ttsoftware.co.uk> wrote:
>
> You can specify an alternative comparison function to the sort method.
> The easiest way to do this is with a lambda...
>
> l.sort (lambda a, b : a[2] < b[2])
>
> For comparison, the following should do the same as the basic sort
> (but slower)...
>
> l.sort (lambda a, b : a < b)
>

Is this something that changed in python 2.0?  The above example
does not work under 1.5.2 where, according to 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.

So the answer to the original question would be:

l.sort (lambda a, b : a[2] - b[2])

-greg


Sent via Deja.com http://www.deja.com/
Before you buy.



More information about the Python-list mailing list