Possible Duplicate:
Does Python have a built in function for string natural sort?
I have a list of strings containing numbers and I cannot find a good way to sort them.
For example I get something like this:
something1
something12
something17
something2
something25
something29
with the sort()
method.
I know that I probably need to extract the numbers somehow and then sort the list but I have no idea how to do it in the most simple way.
Perhaps you are looking for human sorting (also known as natural sorting):
yields
PS. I've changed my answer to use Toothy's implementation of natural sorting (posted in the comments here) since it is significantly faster than my original answer.
If you wish to sort text with floats, then you'll need to change the regex from one that matches ints (i.e.
(\d+)
) to a regex that matches floats:yields