sort->list

Roeland Rengelink r.b.rigilink at chello.nl
Fri Jul 13 03:51:14 EDT 2001


domi wrote:
> 
> Hello All!
> 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,...]
> 
> Any idea thanks domi

Make sure that you are sorting a list of numbers and not a list of 
strings:

i.e.

>>> alist = ['1', '2', '11', '12', '22']
>>> alist.sort()
>>> alist
['1', '11', '12', '2', '22']
>>> blist = map(int, alist)  # change the strings to ints
>>> blist
[1, 11, 12, 2, 22]
>>> blist.sort()
>>> blist  
[1, 2, 11, 12, 22]

Hope this helps,

Roeland


-- 
r.b.rigilink at chello.nl

"Half of what I say is nonsense. Unfortunately I don't know which half"



More information about the Python-list mailing list