File tree Expand file tree Collapse file tree 2 files changed +9
-1
lines changed Expand file tree Collapse file tree 2 files changed +9
-1
lines changed Original file line number Diff line number Diff line change @@ -245,4 +245,8 @@ def sort_key(string, numeric=False):
245
245
# scripts. Replace numbers in the sort string with their latin equivalent.
246
246
if numeric and (IS_MACOS or IS_WIN ):
247
247
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 ' , '' ))
Original file line number Diff line number Diff line change @@ -82,7 +82,11 @@ def test_sort_key(self):
82
82
i18n .setup_gettext (localedir , 'de' )
83
83
self .assertTrue (i18n .sort_key ('äb' ) < i18n .sort_key ('ac' ))
84
84
self .assertTrue (i18n .sort_key ('foo002' ) < i18n .sort_key ('foo1' ))
85
+ self .assertTrue (i18n .sort_key ('002 foo' ) < i18n .sort_key ('1 foo' ))
85
86
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 ))
86
90
87
91
def test_sort_key_numbers_different_scripts (self ):
88
92
i18n .setup_gettext (localedir , 'en' )
You can’t perform that action at this time.
0 commit comments