Python 1.5.2 list sorting bug

Brian Kelley kelley at bioreason.com
Fri Oct 29 11:36:54 EDT 1999


Tim Peters wrote:

> [Brian Kelley, finding a phantom bug, and then embracing a
>  phantom solution <wink>]
>
>
> OK, everyone has beat you soundly about the head and shoulders for not using
> a 3-outcome sort function -- and they were right to do so.  I should really
> keep quiet at this point, but I can't resist:

Yeah, I feel properly chastised.  Especially since Andrew wrote his excellent
mini-sorting HOWTO.  The caveat emptor is that I found this "phantom bug" on
really old code.  And so I can give a little something back that we have found
useful:  our desired method of sorting these days is something like:

sort_list = []
for a in objects:
 sort_list.append( (a.attribute, objects.index(a), a) )

sort_list.sort()

Tuple sorting is much faster than lambdas and this prevents several problems we
have with breaking ties especially since the sorting routine has changed from
1.5.1 and 1.5.2 which are both deteminisitic, just deterministic in a
completely different way.  It  is very important for us to have the same
results for a sort which means a standard way of breaking ties.

Looking back, it was unfortunate that I said "bug", what I should have said was
"difference" or better yet "has the sorting routine changed between 1.5.1 and
1.5.2?".  But this was posted after many hours tracking down the problem.  So
it *was* a bug, just *my* bug that happened to work in python 1.5.1 but not
python 1.5.2.

Needless to say, just becuase something works, doesn't mean it's right.
Although stupid comments can lead to interesting discussions (ala the
difference in sorting between 1.5.1 and 1.5.2 which, actually, was my question
in the first place although even I didn't know it at the time.)

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