Python for whatever reason does not come with a built-in way to have natural sorting (meaning 1, 2, 10 instead of 1, 10, 2), so you have to write it yourself:
import re
def sorted_alphanumeric(data):
convert = lambda text: int(text) if text.isdigit() else text.lower()
alphanum_key = lambda key: [ convert(c) for c in re.split('([0-9]+)', key) ]
return sorted(data, key=alphanum_key)
You can now use this function to sort a list:
dirlist = sorted_alphanumeric(os.listdir(...))
PROBLEMS:
In case you use the above function to sort strings (for example folder names) and want them sorted like Windows Explorer does, it will not work properly in some edge cases.
This sorting function will return incorrect results on Windows, if you have folder names with certain 'special' characters in them. For example this function will sort 1, !1, !a, a
, whereas Windows Explorer would sort !1, 1, !a, a
.
So if you want to sort exactly like Windows Explorer does in Python you have to use the Windows built-in function StrCmpLogicalW via ctypes (this of course won't work on Unix):
from ctypes import wintypes, windll
from functools import cmp_to_key
def winsort(data):
_StrCmpLogicalW = windll.Shlwapi.StrCmpLogicalW
_StrCmpLogicalW.argtypes = [wintypes.LPWSTR, wintypes.LPWSTR]
_StrCmpLogicalW.restype = wintypes.INT
cmp_fnc = lambda psz1, psz2: _StrCmpLogicalW(psz1, psz2)
return sorted(data, key=cmp_to_key(cmp_fnc))
This function is slightly slower than sorted_alphanumeric()
.
Bonus: winsort
can also sort full paths on Windows.
Alternatively, especially if you use Unix, you can use the natsort
library (pip install natsort
) to sort by full paths in a correct way (meaning subfolders at the correct position).
You can use it like this to sort full paths:
from natsort import natsorted, ns
dirlist = natsorted(dirlist, alg=ns.PATH | ns.IGNORECASE)
Starting with version 7.1.0 natsort supports os_sorted
which internally uses either the beforementioned Windows API or Linux sorting and should be used instead of natsorted()
.