If you have a list of strings containing numbers and you wanna sort in it the human sorting or natural sorting then you might like this post
For example I get something like this:
something1 something12 something17 something2 something25 something29
I know that you probably need to extract the numbers somehow and then sort the list but you have no idea how to do it in the most simple way.
The following is what you are looking for
import re def atoi(text): return int(text) if text.isdigit() else text def natural_keys(text): ''' alist.sort(key=natural_keys) sorts in human order http://nedbatchelder.com/blog/200712/human_sorting.html (See Toothy's implementation in the comments) ''' return [ atoi(c) for c in re.split('(\d+)', text) ] alist=[ "something1", "something12", "something17", "something2", "something25", "something29"] alist.sort(key=natural_keys) print(alist)
['something1', 'something2', 'something12', 'something17', 'something25', 'something29']