Search This Blog

Wednesday, April 7, 2010

Sorting numbers in string format with Python

Programmer Question

I have a list that has some chapter numbers in string.
When I sort the keys using keys function, it gives me wrong results.




keys = ['1.1', '1.2', '2.1', '10.1']
keys.sort()
print keys

['1.1', '1.2', '10.1', '2.1']


How can I use the sort function to get




['1.1', '1.2', '2.1', '10.1']


What if the array has something like this?




['1.1.1', '1.2.1', '10.1', '2.1'] -> ['1.1.1','1.2.1','2.1','10.1']




Find the answer here

No comments:

Post a Comment

Related Posts with Thumbnails