Quantcast
Channel: Sort a list of names containing numbers by the numeric value - Stack Overflow
Viewing all articles
Browse latest Browse all 3

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

$
0
0

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



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>