sort->list

Brian Quinlan brian at sweetapp.com
Fri Jul 13 04:06:20 EDT 2001


Domi wrote:
> Does somebody know how to sort a numeric list in the right way?
> The sort method sort a list in this way:
> [1,10,11,12,13,14,15,16,17,18,19,2,20,21,...]
> 
> but I need a list sort like this:
> [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,...]

It looks like you list is actually of strings rather than integers. That
is why it is being sorted alphabetically rather than numerically. Try
converting the list to integers first i.e.

testList = ['1', '10', '2', '15']
testList = map(int, testList)
testList.sort()






More information about the Python-list mailing list