Sort

Steve Horne sh at ttsoftware.co.uk
Wed Dec 6 12:07:32 EST 2000


On Wed, 06 Dec 2000 14:30:42 GMT, Greg Landrum <glandrum at my-deja.com>
wrote:

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

You are right - my bad - I should have used the cmp function, which
gives the correct -1, 0 or 1 result.

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

The < operator only returns 0 or 1, so the list would only be partly
sorted - though sadly enough to fool me on my quick memory-refreshing
test.

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

Surely this doesn't always work either? Even for integers, you can get
values other that -1, 0 and 1. And what about strings, nested lists,
etc?

-- 
Steve Horne
Home : steve at lurking.demon.co.uk
Work : sh at ttsoftware.co.uk



More information about the Python-list mailing list