Skip to content

Instantly share code, notes, and snippets.

@petrushev
Created June 21, 2012 11:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save petrushev/2965367 to your computer and use it in GitHub Desktop.
Save petrushev/2965367 to your computer and use it in GitHub Desktop.
Sort list of strings by a specific locale
# -*- 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