Skip to content

Commit

Permalink
Workaround for purely numeric strings not being sorted naturally on m…
Browse files Browse the repository at this point in the history
…acOS
  • Loading branch information
phw committed Aug 10, 2024
1 parent d7a6613 commit 233fca0
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
6 changes: 5 additions & 1 deletion picard/i18n.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,4 +245,8 @@ def sort_key(string, numeric=False):
# scripts. Replace numbers in the sort string with their latin equivalent.
if numeric and (IS_MACOS or IS_WIN):
string = re.sub(r'\d', _digit_replace, string)
return collator.sortKey(string.replace('\0', ''))

# On macOS numeric sorting of strings entirely consisting of numeric characters fails
# and always sorts alphabetically (002 < 1). Always prefix with an alphabeticcharacter
# to work around that.
return collator.sortKey('a' + string.replace('\0', ''))
4 changes: 4 additions & 0 deletions test/test_i18n.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,11 @@ def test_sort_key(self):
i18n.setup_gettext(localedir, 'de')
self.assertTrue(i18n.sort_key('äb') < i18n.sort_key('ac'))
self.assertTrue(i18n.sort_key('foo002') < i18n.sort_key('foo1'))
self.assertTrue(i18n.sort_key('002 foo') < i18n.sort_key('1 foo'))
self.assertTrue(i18n.sort_key('foo1', numeric=True) < i18n.sort_key('foo002', numeric=True))
self.assertTrue(i18n.sort_key('004', numeric=True) < i18n.sort_key('5', numeric=True))
self.assertTrue(i18n.sort_key('0042', numeric=True) < i18n.sort_key('50', numeric=True))
self.assertTrue(i18n.sort_key('5', numeric=True) < i18n.sort_key('0042', numeric=True))

def test_sort_key_numbers_different_scripts(self):
i18n.setup_gettext(localedir, 'en')
Expand Down

0 comments on commit 233fca0

Please sign in to comment.