อัลกอริทึม

อัลกอริทึมการเรียงลำดับ

อัลกอริธึมมาตรฐานสำหรับเปรียบเทียบและเรียงลำดับสตริง Unicode โดยใช้การเปรียบเทียบหลายระดับ: อักขระฐาน → สำเนียง → ตัวพิมพ์ → ตัวตัดสิน ปรับแต่งตาม locale ได้

· Updated

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, v and w are treated as equivalent at the primary level (both sort before x)
  • In Spanish (traditional), ch sorts as a unit after all c words
  • 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

คำศัพท์ที่เกี่ยวข้อง

เพิ่มเติมใน อัลกอริทึม

Case Folding

Mapping characters to a common case form for case-insensitive comparison. More comprehensive …

Grapheme Cluster Boundary

Rules (UAX#29) for determining where one user-perceived character ends and another begins. …

NFC (Canonical Composition)

Normalization Form C: แยกส่วนแล้วรวมใหม่แบบ canonical ได้รูปแบบที่สั้นที่สุด แนะนำสำหรับการจัดเก็บและแลกเปลี่ยนข้อมูล เป็นรูปแบบมาตรฐานของเว็บ

NFD (Canonical Decomposition)

Normalization Form D: แยกส่วนอย่างสมบูรณ์โดยไม่รวมใหม่ ใช้โดยระบบไฟล์ macOS HFS+ é (U+00E9) → e + …

NFKC (Compatibility Composition)

Normalization Form KC: แยกส่วนแบบ compatibility แล้วรวมแบบ canonical รวมอักขระที่มีลักษณะคล้ายกัน (fi→fi, ²→2, Ⅳ→IV) ใช้สำหรับการเปรียบเทียบตัวระบุ

NFKD (Compatibility Decomposition)

Normalization Form KD: แยกส่วนแบบ compatibility โดยไม่รวมใหม่ เป็นการ normalize ที่เข้มงวดที่สุด สูญเสียข้อมูลการจัดรูปแบบมากที่สุด

String Comparison

Comparing Unicode strings requires normalization (NFC/NFD) and optionally collation (locale-aware sorting). Binary …

การทำให้เป็นมาตรฐาน

กระบวนการแปลงข้อความ Unicode เป็นรูปแบบ canonical มาตรฐาน มี 4 รูปแบบ: NFC (รวม), NFD (แยก), …

การยกเว้นการประกอบ

อักขระที่ถูกยกเว้นจากการรวมแบบ canonical (NFC) เพื่อป้องกันการแตกย่อยแบบ non-starter และรับประกันความเสถียรของอัลกอริทึม ระบุไว้ใน CompositionExclusions.txt

การแบ่งส่วนข้อความ

อัลกอริธึมสำหรับค้นหาขอบเขตในข้อความ: ขอบเขต grapheme cluster, คำ และประโยค มีความสำคัญสำหรับการเลื่อนเคอร์เซอร์ การเลือกข้อความ และการประมวลผลข้อความ