漢字在記憶體中以多位元組編碼儲存 (GBK/UTF-8/UTF-16)。直接用 `strcmp` 比較的是位元組序,而非語義上的「大小」(拼音/筆劃/部首)。
#include <string.h>
int cmp = strcmp(str1, str2); // 返回 <0, 0, >0
// 等價於逐位元組比較 unsigned char 值
#include <wchar.h>
#include <locale.h>
setlocale(LC_ALL, "zh_CN.utf8"); // 或 "chs" (Windows)
wchar_t ws1[] = L"中文";
wchar_t ws2[] = L"英文";
int cmp = wcscmp(ws1, ws2); // 依當前 locale 排序規則
跨平台、支援 Unicode 校準算法 (UCA)、可自定義權重 (拼音/筆劃/部首/注音)。
// 概念代碼
#include <unicode/ucol.h>
UCollator *coll = ucol_open("zh_CN", &status);
UCollationResult res = ucol_strcoll(coll, ustr1, len1, ustr2, len2);
// res: UCOL_LESS / UCOL_EQUAL / UCOL_GREATER
#include <windows.h>
int cmp = CompareStringEx(LOCALE_NAME_USER_DEFAULT, 0,
str1, -1, str2, -1, NULL, NULL, 0);
// 返回 CSTR_LESS_THAN (1), CSTR_EQUAL (2), CSTR_GREATER_THAN (3)