Python 1.5.2 list sorting bug

Eric Jacobs x at x.x
Thu Oct 28 21:49:15 EDT 1999


Brian Kelley wrote:
> 
> actually a much quicker test is:
> 
> Python 1.5.2 (#1, Oct 27 1999, 17:21:07) [C] on irix646
> Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
> >>> a = [3,4,5,1,2]
> >>> a.sort(lambda x,y: x>y)
> >>> a
> [3, 4, 5, 1, 2]
> >>>

Strange how it works on 1.5.1!! It shouldn't; what you really
mean is:

Python 1.5.2 (#0, Oct 28 1999, 20:40:08)  [GCC cygnus-2.7.2-970404] on windows
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>> a = [3,4,5,1,2]
>>> a.sort(lambda x,y: x-y)
>>> a
[1, 2, 3, 4, 5]

The sort function is like __cmp__: it's used to detect all
of <, =, and >. That said, the current sorting algorithm
only tests for <. So a.sort(lambda x,y: -(x<y)) would
work as well. But, best play it by the specifications.




More information about the Python-list mailing list