下面的 Python 代码应该做排序的东西:
import re
from glob import glob
files = sorted([ file[:-4] for file in glob('*.txt') ])
chunked = []
for i, file in enumerate(files):
chunks = re.split('(\d+)', file)
for j, chunk in enumerate(chunks):
if chunk.isdigit():
chunks[j] = int(chunk)
chunked.append((chunks, i))
for chunk, i in sorted(chunked):
print(files[i])
看起来像这样的目录:
$ ls
01aa.txt 21a.txt aa01bb.txt aa10bb.txt
21aa.txt 2aa.txt aa023bb.txt aa1bb.txt
它输出:
01aa.txt
2aa.txt
21a.txt
21aa.txt
aa01bb.txt
aa1bb.txt
aa10bb.txt
aa023bb.txt
这与输出简单的sort
.
$ ls | sort
01aa.txt
21aa.txt
21a.txt
2aa.txt
aa01bb.txt
aa023bb.txt
aa10bb.txt
aa1bb.txt