Add pathclass.natural_sorter.

This commit is contained in:
voussoir 2022-08-21 13:58:44 -07:00
parent 2996283344
commit 7374ffe283
No known key found for this signature in database
GPG key ID: 5F7554F8C26DACCB

View file

@ -1,5 +1,6 @@
import glob
import os
import re
import shutil
_glob = glob
@ -729,6 +730,22 @@ def glob_patternize(piece):
break
return piece
def natural_sorter(path):
'''
This function is used as the `key` argument in
paths.sort(key=pathclass.natural_sorter).
Used for sorting strings in 'natural' order instead of lexicographic order,
so that you get 1 2 3 4 5 6 7 8 9 10 11 12 13 ...
instead of 1 10 11 12 13 2 3 4 5 ...
Thank you Mark Byers
http://stackoverflow.com/a/11150413
'''
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.absolute_path)]
return alphanum_key(path)
def normalize_drive(name):
if type(name) is Drive:
return name