Add pathclass.natural_sorter.

master
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
1 changed files with 17 additions and 0 deletions

View File

@ -1,5 +1,6 @@
import glob import glob
import os import os
import re
import shutil import shutil
_glob = glob _glob = glob
@ -729,6 +730,22 @@ def glob_patternize(piece):
break break
return piece 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): def normalize_drive(name):
if type(name) is Drive: if type(name) is Drive:
return name return name