Python Glossary Русский

all Python 3localefunction

locale.strcoll()

string collation

Compares two strings under the LC_COLLATE rules, returning a negative number, zero or a positive number, like cmp in old Python.

Syntax

locale.strcoll(string1, string2)

Until setlocale() is called LC_COLLATE is 'C', and the order follows code points: 'ё' ends up after 'я' and capitals come before lowercase. It returns a number, not a bool, and goes into sorted() through functools.cmp_to_key; key=locale.strxfrm is faster for sorting. Compare the sign of the result, not its exact value, which varies by platform.

Examples

import functools
import locale
print(locale.strcoll('a', 'B') > 0)  # → True — в C заглавные идут раньше
words = ['ёж', 'яблоко', 'еда']
print(sorted(words, key=functools.cmp_to_key(locale.strcoll)))  # → ['еда', 'яблоко', 'ёж']

See also

Open in the full glossarydocs.python.orgReport an inaccuracy