Quantcast
Viewing all articles
Browse latest Browse all 3

Answer by Eugene Yarmash for Sort a list of names containing numbers by the numeric value

You can use a custom sorting function to extract the number from each element of the list and use it as the comparison key. You'd need to convert it to int to ensure it compares like number:

>>> lst = ['100.avi', '10000.avi', '10002.avi', '1003.avi']
>>> sorted(lst, key=lambda x: int(x.split('.', 1)[0]))
['100.avi', '1003.avi', '10000.avi', '10002.avi']

Viewing all articles
Browse latest Browse all 3

Trending Articles