sorted() erraticly fails to sort string numbers

uuid M8R-r1c6h51 at mailinator.com
Tue Apr 28 09:47:50 CEST 2009


I would be very interested in a logical explanation why this happens on 
python 2.5.1:

In order to sort an etree by the .text value of one child, I adapted 
this snippet from effbot.org:

> import xml.etree.ElementTree as ET
> 
> tree = ET.parse("data.xml")
> 
> def getkey(elem):
>     return elem.findtext("number")
> 
> container = tree.find("entries")
> 
> container[:] = sorted(container, key=getkey)
> 
> tree.write("new-data.xml")

While working with a moderately sized xml file (2500 entries to sort 
by), I found that a few elements were not in order. It seems that 
numbers with seven digits were sorted correctly, while those with six 
digits were just added at the end.

I fixed the problem by converting the numbers to int in the callback:

> def getkey(elem):
>     return int(elem.findtext("number"))

So to my naive mind, it seems as if there was some error with the 
sorted() function. Would anyone be as kind as to explain why it could 
be happening? Thanks in advance!




More information about the Python-list mailing list