Unicode 排序算法 (UCA)
通过多级比较(基础字符→变音符号→大小写→决胜符)对Unicode字符串进行比较和排序的标准算法,支持区域设置自定义。
Sorting Strings Across Languages
ASCII sorting is simple: compare byte values. But for multilingual text, byte-order sorting produces absurd results: "ä" sorts after "z" in ASCII, "纳" appears nowhere near "那" despite being phonetically similar, and "naïve" sorts far from "naive". The Unicode Collation Algorithm (UCA), specified in Unicode Technical Standard #10, provides a framework for language-aware sorting.
Multi-Level Comparison Keys
The UCA uses up to four comparison levels, applied in order. If two strings are equal at level 1, level 2 is consulted, and so on:
| Level | Name | Distinguishes |
|---|---|---|
| L1 | Primary | Base characters (a ≠ b, a = á at this level) |
| L2 | Secondary | Accents / diacritics (a ≠ á) |
| L3 | Tertiary | Case / variants (a ≠ A, AK ≠ AK) |
| L4 | Quaternary | Punctuation, special marks |
This means a case-insensitive, accent-insensitive search operates at level 1: "naïve" equals "naive" equals "NAIVE". A case-insensitive but accent-sensitive sort uses levels 1–2: "naive" < "naïve" because they differ at level 2.
import locale
# On Linux/macOS with proper locale support
locale.setlocale(locale.LC_ALL, "en_US.UTF-8")
words = ["naive", "naïve", "NAIVE", "résumé", "resume"]
words.sort(key=locale.strxfrm)
print(words) # locale-aware sort
# For robust multilingual sorting, use the PyICU library
# pip install pyicu
from icu import Collator, Locale
collator = Collator.createInstance(Locale("en_US"))
words.sort(key=collator.getSortKey)
CLDR Tailorings
The UCA defines a default collation order (DUCET — Default Unicode Collation Element Table), but different locales require different sort orders. The Unicode Common Locale Data Repository (CLDR) provides tailorings that modify the UCA for specific languages:
- In Swedish,
vandware treated as equivalent at the primary level (both sort beforex) - In Spanish (traditional),
chsorts as a unit after allcwords - In German (phone book order),
ä=ae, so "Ärger" sorts with "Ärger" near "Aero" - In Japanese, hiragana and katakana may be treated as equivalent at level 1
Without CLDR tailorings, sorting Swedish or German text with the default UCA produces results that feel wrong to native speakers.
Practical Python Sorting
# Simple locale-aware sort (depends on OS locale support)
import locale
locale.setlocale(locale.LC_COLLATE, "de_DE.UTF-8")
german_words = ["Äpfel", "Apfel", "Zorn", "außen"]
german_words.sort(key=locale.strxfrm)
# Check if a string needs normalization before collation
import unicodedata
def collation_key(s: str) -> str:
return locale.strxfrm(unicodedata.normalize("NFC", s))
Quick Facts
| Property | Value |
|---|---|
| Specification | Unicode Technical Standard #10 (UTS #10) |
| Full name | Unicode Collation Algorithm (UCA) |
| Default table | DUCET (Default Unicode Collation Element Table) |
| Locale data | CLDR (Common Locale Data Repository) |
| Comparison levels | 4 levels (primary, secondary, tertiary, quaternary) |
| Python (basic) | locale.strxfrm() |
| Python (full ICU) | PyICU library — Collator.createInstance() |
| Java / ICU | java.text.Collator, com.ibm.icu.text.Collator |
相关术语
算法 中的更多内容
Mapping characters to a common case form for case-insensitive comparison. More comprehensive …
Rules (UAX#29) for determining where one user-perceived character ends and another begins. …
规范化形式C:先分解再规范合成,生成最短形式,推荐用于数据存储和交换,是Web标准形式。
规范化形式D:完全分解而不重新合成,macOS HFS+文件系统使用此形式。é(U+00E9)→ e + ◌́(U+0065 + U+0301)。
规范化形式KC:兼容分解后再规范合成,合并视觉上相似的字符(fi→fi、²→2、Ⅳ→IV),用于标识符比较。
规范化形式KD:兼容分解而不重新合成,是最激进的规范化方式,会丢失最多的格式信息。
Comparing Unicode strings requires normalization (NFC/NFD) and optionally collation (locale-aware sorting). Binary …
利用字符双向类别和明确方向覆盖,确定混合方向文本(如英语+阿拉伯语)显示顺序的算法。
根据字符属性、CJK词边界和换行时机,确定文本可换至下一行位置的规则。
查找文本中各类边界的算法:字素簇、词和句子边界,对光标移动、文本选择和文本处理至关重要。