Skip to content

Commit 7531349

Browse files
committed
Use a 'natural' sort order for UILists
Pretty much only affects the Saves and Screenshots tabs. Instead of save1.ess, save10.ess, save2.ess, etc., we now show save1.ess, save2.ess, save10.ess, etc., similar to most file managers (most prominently Windows Explorer).
1 parent 430e58b commit 7531349

2 files changed

Lines changed: 15 additions & 1 deletion

File tree

Mopy/bash/balt.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2002,7 +2002,7 @@ def _SortItems(self, col, reverse=False, items=None, sortSpecial=True):
20022002
items = items if items is not None else self.data_store.keys()
20032003
def key(k): # if key is None then keep it None else provide self
20042004
k = self._sort_keys[k]
2005-
return k if k is None else partial(k, self)
2005+
return bolt.natural_key() if k is None else partial(k, self)
20062006
defaultKey = key(self._default_sort_col)
20072007
defSort = col == self._default_sort_col
20082008
# always apply default sort

Mopy/bash/bolt.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1926,6 +1926,20 @@ def loadFile(self, path, progress, lang=u'english'):
19261926
deprint(u'Error loading string file:', path.stail, traceback=True)
19271927
return
19281928

1929+
#------------------------------------------------------------------------------
1930+
_digit_re = re.compile(u'([0-9]+)')
1931+
1932+
def natural_key():
1933+
"""Returns a sort key for 'natural' sort order, i.e. similar to how most
1934+
file managers display it - a1.png, a2.png, a10.png. Can handle both strings
1935+
and paths. Inspired by
1936+
https://blog.codinghorror.com/sorting-for-humans-natural-sort-order/."""
1937+
def _to_cmp(sub_str):
1938+
"""Helper function that prepares substrings for comparison."""
1939+
return int(sub_str) if sub_str.isdigit() else sub_str.lower()
1940+
return lambda curr_str: [_to_cmp(s) for s in
1941+
_digit_re.split(u'%s' % curr_str)]
1942+
19291943
# WryeText --------------------------------------------------------------------
19301944
codebox = None
19311945
class WryeText(object):

0 commit comments

Comments
 (0)