Python 1.5.2 list sorting bug

Brian Kelley kelley at bioreason.com
Thu Oct 28 18:05:50 EDT 1999


Hans Nowak pointed out the following:
>Are you aware that this function is not a correct comparison function
> for sort? Or did I miss the point of your message entirely?

>As far as I know, the sorting is "broken" because your comparison
>returns 0 and 1, while sort expects a function that returns -1, 0, or
1.

It turns out that Python 1.5.1 can sort using return values of 0 and 1
while that behavior has been modified in Python 1.5.2.  Replacing "<"
with "-" works in both versions.  Seems it was a bug in the script that
had the desired behavior.

I actually have no idea where I picked the idiom up, but I got used to

list.sort(lambda x,y: x>y)
and
list.sort(lambda x,y: x<y)

for increasing and decreasing sorts.  Thats what I get for playing the
interpreter.   I really have no excuse since I was with Andrew when he
wrote the mini-sorting how-to.


--
Brian Kelley          w 505 995-8188
Bioreason, Inc        f 505 995-8186
309 Johnson Av
Santa Fe, NM 87501    kelley at bioreason.com







More information about the Python-list mailing list