Created
April 22, 2015 18:55
-
-
Save janpipek/70e3d6aad5331837d07d to your computer and use it in GitHub Desktop.
sort using locale in windows (>= Vista???)
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
import ctypes | |
from ctypes import windll | |
kernel32 = windll.LoadLibrary("kernel32") | |
import functools | |
def compare(str1, str2, locale="cs-CZ"): | |
res = kernel32.CompareStringEx( | |
ctypes.c_wchar_p(locale), | |
ctypes.c_ulong(0), | |
ctypes.c_wchar_p(str1), | |
len(str1), | |
ctypes.c_wchar_p(str2), | |
len(str2), | |
None, | |
None, | |
ctypes.c_long(0) | |
) | |
return res - 2 | |
def key_comparer(locale="cs-CZ"): | |
specific_compare = functools.partial(compare, locale=locale) | |
return functools.cmp_to_key(specific_compare) | |
def sort_with_locale(iterable, locale="cs-CZ"): | |
return sorted(iterable, key=key_comparer(locale)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment