From 054f63f6f956baadaffdb9bff5bffca7360ae76d Mon Sep 17 00:00:00 2001 From: Lieven Govaerts Date: Fri, 18 Jan 2019 22:51:11 +0100 Subject: [PATCH] Show the file list in natural sorted order ( f1->f8->f9->f10 instead of f1->f10->f8->f9 ). * labelImg.py (natural_sort): New function, copied from S.O.: https://stackoverflow.com/questions/4836710/does-python-have-a-built-in-function-for-string-natural-sort?answertab=votes#tab-top (scanAllImages): Return the image file list in natural sorted order. --- labelImg.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/labelImg.py b/labelImg.py index c5d05c54..0115aa3a 100755 --- a/labelImg.py +++ b/labelImg.py @@ -59,6 +59,17 @@ def util_qt_strlistclass(): return QStringList if have_qstring() else list +def natural_sort(list, key=lambda s:s): + """ + Sort the list into natural alphanumeric order. + """ + def get_alphanum_key_func(key): + convert = lambda text: int(text) if text.isdigit() else text + return lambda s: [convert(c) for c in re.split('([0-9]+)', key(s))] + sort_key = get_alphanum_key_func(key) + list.sort(key=sort_key) + + class WindowMixin(object): def menu(self, title, actions=None): @@ -1140,7 +1151,7 @@ class MainWindow(QMainWindow, WindowMixin): relativePath = os.path.join(root, file) path = ustr(os.path.abspath(relativePath)) images.append(path) - images.sort(key=lambda x: x.lower()) + natural_sort(images, key=lambda x: x.lower()) return images def changeSavedirDialog(self, _value=False):