Skip to content

Commit 233fca0

Browse files
committed
Workaround for purely numeric strings not being sorted naturally on macOS
1 parent d7a6613 commit 233fca0

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

picard/i18n.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,4 +245,8 @@ def sort_key(string, numeric=False):
245245
# scripts. Replace numbers in the sort string with their latin equivalent.
246246
if numeric and (IS_MACOS or IS_WIN):
247247
string = re.sub(r'\d', _digit_replace, string)
248-
return collator.sortKey(string.replace('\0', ''))
248+
249+
# On macOS numeric sorting of strings entirely consisting of numeric characters fails
250+
# and always sorts alphabetically (002 < 1). Always prefix with an alphabeticcharacter
251+
# to work around that.
252+
return collator.sortKey('a' + string.replace('\0', ''))

test/test_i18n.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,11 @@ def test_sort_key(self):
8282
i18n.setup_gettext(localedir, 'de')
8383
self.assertTrue(i18n.sort_key('äb') < i18n.sort_key('ac'))
8484
self.assertTrue(i18n.sort_key('foo002') < i18n.sort_key('foo1'))
85+
self.assertTrue(i18n.sort_key('002 foo') < i18n.sort_key('1 foo'))
8586
self.assertTrue(i18n.sort_key('foo1', numeric=True) < i18n.sort_key('foo002', numeric=True))
87+
self.assertTrue(i18n.sort_key('004', numeric=True) < i18n.sort_key('5', numeric=True))
88+
self.assertTrue(i18n.sort_key('0042', numeric=True) < i18n.sort_key('50', numeric=True))
89+
self.assertTrue(i18n.sort_key('5', numeric=True) < i18n.sort_key('0042', numeric=True))
8690

8791
def test_sort_key_numbers_different_scripts(self):
8892
i18n.setup_gettext(localedir, 'en')

0 commit comments

Comments
 (0)