Skip to content

Commit 78331a8

Browse files
committed
Sort numbers in game names numerically
1 parent bd33f86 commit 78331a8

File tree

2 files changed

+16
-8
lines changed

2 files changed

+16
-8
lines changed

pysollib/gamedb.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#
2222
# ---------------------------------------------------------------------------##
2323

24+
import re
2425
from importlib import util
2526

2627
import pysollib.settings
@@ -848,6 +849,13 @@ def register(self, gi):
848849
self.callback()
849850
self._num_games += 1
850851

852+
def getNaturalSortKey(self, text):
853+
# Sort numbers numerically, and strings alphabetically
854+
def convert(text):
855+
return int(text) if text.isdigit() else text.lower()
856+
857+
return [convert(c) for c in re.split(r'(\d+)', text)]
858+
851859
#
852860
# access games database - we do not expose hidden games
853861
#
@@ -865,17 +873,17 @@ def getGamesIdSortedByName(self):
865873
if self.__games_by_name is None:
866874
l1, l2, l3 = [], [], []
867875
for id, gi in self.__games.items():
868-
name = gi.name .lower()
869-
l1.append((name, id))
876+
name = gi.name.lower()
877+
l1.append((self.getNaturalSortKey(name), id))
870878
if gi.name != gi.short_name:
871879
name = gi.short_name.lower()
872-
l2.append((name, id))
880+
l2.append((self.getNaturalSortKey(name), id))
873881
for n in gi.altnames:
874882
name = n.lower()
875-
l3.append((name, id, n))
876-
l1.sort()
877-
l2.sort()
878-
l3.sort()
883+
l3.append((self.getNaturalSortKey(name), id, n))
884+
l1.sort(key=lambda x: x[0])
885+
l2.sort(key=lambda x: x[0])
886+
l3.sort(key=lambda x: x[0])
879887
self.__games_by_name = tuple(i[1] for i in l1)
880888
self.__games_by_short_name = tuple(i[1] for i in l2)
881889
self.__games_by_altname = tuple(i[1:] for i in l3)

pysollib/tile/selectgame.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -672,7 +672,7 @@ def performSearch(self):
672672
for altname in game.altnames:
673673
if self.app.checkSearchString(self.criteria.name, altname):
674674
results.append(altname)
675-
results.sort(key=lambda x: x.lower())
675+
results.sort(key=self.app.gdb.getNaturalSortKey)
676676
pos = 0
677677
for result in results:
678678
self.list.insert(pos, result)

0 commit comments

Comments
 (0)