Skip to content

Commit 054f63f

Browse files
lgovtzutalin
authored andcommitted
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.
1 parent ba12d85 commit 054f63f

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

labelImg.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,17 @@ def util_qt_strlistclass():
5959
return QStringList if have_qstring() else list
6060

6161

62+
def natural_sort(list, key=lambda s:s):
63+
"""
64+
Sort the list into natural alphanumeric order.
65+
"""
66+
def get_alphanum_key_func(key):
67+
convert = lambda text: int(text) if text.isdigit() else text
68+
return lambda s: [convert(c) for c in re.split('([0-9]+)', key(s))]
69+
sort_key = get_alphanum_key_func(key)
70+
list.sort(key=sort_key)
71+
72+
6273
class WindowMixin(object):
6374

6475
def menu(self, title, actions=None):
@@ -1140,7 +1151,7 @@ def scanAllImages(self, folderPath):
11401151
relativePath = os.path.join(root, file)
11411152
path = ustr(os.path.abspath(relativePath))
11421153
images.append(path)
1143-
images.sort(key=lambda x: x.lower())
1154+
natural_sort(images, key=lambda x: x.lower())
11441155
return images
11451156

11461157
def changeSavedirDialog(self, _value=False):

0 commit comments

Comments
 (0)