Created
June 21, 2012 11:59
-
-
Save petrushev/2965367 to your computer and use it in GitHub Desktop.
Sort list of strings by a specific locale
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
import locale | |
default_locale = locale.getlocale(locale.LC_COLLATE) | |
def sort_strings(strings, locale_=None): | |
if locale_ is None: | |
return sorted(strings) | |
locale.setlocale(locale.LC_COLLATE, locale_) | |
sorted_strings = sorted(strings, cmp=locale.strcoll) | |
locale.setlocale(locale.LC_COLLATE, default_locale) | |
return sorted_strings | |
test_list = [u'a',u'џ',u'ш'] | |
assert ' '.join( sort_strings(test_list, 'C') ) == u'a ш џ' | |
assert ' '.join( sort_strings(test_list, 'mk_MK.UTF-8') ) == u'a џ ш' | |
assert sort_strings(test_list, default_locale)==sort_strings(test_list) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment