유니코드 정렬 알고리즘 (UCA)
기본 문자 → 악센트 → 대소문자 → 동점 비교자의 다단계 비교를 통해 유니코드 문자열을 비교하고 정렬하는 표준 알고리즘. 로케일 맞춤 설정이 가능합니다.
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: 분해 후 정규 재합성하여 가장 짧은 형식을 생성합니다. 데이터 …
정규화 형식 D: 재합성 없이 완전히 분해합니다. macOS HFS+ 파일 시스템에서 사용됩니다. …
정규화 형식 KC: 호환 분해 후 정규 합성. 시각적으로 유사한 문자를 통합합니다(fi→fi, …
정규화 형식 KD: 재합성 없이 호환 분해. 가장 강력한 정규화 방식으로 서식 …
Comparing Unicode strings requires normalization (NFC/NFD) and optionally collation (locale-aware sorting). Binary …
유니코드 단어 경계 규칙에 따라 결정된 단어 사이의 위치. 단순히 공백으로 분리하는 …
유니코드 규칙에 따른 문장 사이의 위치. 마침표로만 분리하는 것보다 복잡하며, 약어(Mr.), 생략 …
문자 양방향 범주와 명시적 방향 재정의를 사용하여 혼합 방향 텍스트(예: 영어 + …