sorted() erraticly fails to sort string numbers

uuid M8R-r1c6h51 at mailinator.com
Tue Apr 28 10:20:10 CEST 2009


I am at the same time impressed with the concise answer and 
disheartened by my inability to see this myself.
My heartfelt thanks!


On 2009-04-28 10:06:24 +0200, Andre Engels <andreengels at gmail.com> said:

> When sorting strings, including strings that represent numbers,
> sorting is done alphabetically. In this alphabetical order the numbers
> are all ordered the normal way, so two numbers with the same number of
> digits will be sorted the same way, but any number starting with "1"
> will come before any number starting with "2", whether they denote
> units, tens, hundreds or millions. Thus:
> 
> "1" < "15999" < "16" < "2"





More information about the Python-list mailing list