2015-10-19 Natural SortΒΆ

natsort is part of the Anaconda distribution package list. It does something I sometimes needs and which is quite annoying to do about sorting. Here is the results of a default sorting algorithm:

<<<

a = ['a2', 'a9', 'a1', 'a4', 'a10']
print(list(sorted(a)))

>>>

    ['a1', 'a10', 'a2', 'a4', 'a9']

natsort does something different with numbers:

<<<

from natsort import natsorted
a = ['a2', 'a9', 'a1', 'a4', 'a10']
print(natsorted(a))

>>>

    ['a1', 'a2', 'a4', 'a9', 'a10']

It is quite interesting when you to sort version numbers for a module.