Add a new mod named Chinese Character Natural Sort - #5019
Conversation
chinese-logical-sort is a mod for provides natural sorting for Chinese file names.
|
Thanks for the pull request! This repository uses a two-stage review: an AI review that you run yourself, followed by a human review. To get started, comment See the pull request review process for the full details. |
|
/ai-review |
Submission reviewNote: This review was done by Claude. Due to the amount of submissions, doing a fully manual review for each pull request is no longer feasible. Thank you for understanding. Remember: The AI reviewer can be wrong - it may misread code, flag correct code as broken, or suggest changes that make things worse. Treat its findings as suggestions to verify, not instructions to follow blindly. You're responsible for the code you submit, so if a finding doesn't hold up, say so instead of changing working code to satisfy it. Please address the following issues. The items in the collapsed sections are optional, so it's your call whether to address them. The idea is sound and there's no existing mod that does this, but the current implementation has a hard hang, changes comparison semantics for non-Chinese text, and hooks two of the most fundamental Windows APIs in every process. Details below. 1. Infinite loop (process hang) on file names starting with
In if (val1 != -1 && val2 != -1 && val1 != val2) { ... }
else {
if (l1 == l2 && wcsncmp(lpString1 + i, lpString2 + j, l1) == 0) {
i += l1; j += l2; continue; // l1 == l2 == 0 -> no progress
}
Two fixes are needed:
if (l1 == 0 || l2 == 0) return 0; // nothing consumed -> defer to the OS2. The hooks change ordering for plain ASCII/Arabic-digit strings, in every process
These APIs aren't only used for display sorting — Suggested fix: in the 3.
if (towlower(lpString1[i]) == towlower(lpString2[j])) { i++; j++; continue; }So a case-sensitive comparison ( Minimum fix: use an exact 4. The comparator isn't transitive, so the resulting order can be arbitrary Because the mod decides some pairs itself and delegates the rest to the OS, the combined comparator has cycles. Concrete example with the
The clean fix is to make the mod produce a sort key instead of a partial verdict: rewrite each string into a canonical form (recognized Chinese numeral runs → zero-padded ASCII digits, 5. Reconsider hooking Explorer and the common file dialogs sort file names through Please check whether the 6. The
This isn't only cosmetic: Additionally, the total += (total + section + temp) * 100000000; // should be: total = (...)
Suggested fix: } else if (c == L'万' || c == L'萬') {
if (section == 0 && temp == 0) temp = 1;
total += (section + temp) * 10000;
section = 0; temp = 0;
found = true;
} else if (c == L'亿' || c == L'億') {
if (total == 0 && section == 0 && temp == 0) temp = 1;
total = (total + section + temp) * 100000000;
section = 0; temp = 0;
found = true;
}Also, 7. Ordinary Chinese words are treated as sequence markers The mod applies its ordering to any recognized character anywhere in the name, with no requirement that it actually be a sequence number. Suggestions: require a numbering context before applying the special ordering (the numeral run is delimited by separators/extension boundaries, or preceded by Optional improvements
Minor polish — none of this affects users, so it's your call.
Functionality notes
Non-critical observations and ideas about the feature behavior itself.
Next steps:
See the review process for details. |
Claude pointed out some of my errors, and I corrected them.
|
/ai-review |
Submission reviewNote: This review was done by Claude. Due to the amount of submissions, doing a fully manual review for each pull request is no longer feasible. Thank you for understanding. Remember: The AI reviewer can be wrong - it may misread code, flag correct code as broken, or suggest changes that make things worse. Treat its findings as suggestions to verify, not instructions to follow blindly. You're responsible for the code you submit, so if a finding doesn't hold up, say so instead of changing working code to satisfy it. Please address the following issues. The items in the collapsed sections are optional, so it's your call whether to address them. The parser itself is solid (overflow-checked, the gates keep false positives down, the reentrancy guard correctly prevents 1. The mod overrides Windows' ordering for names that have nothing in common, contradicting the documented fallback. In if (ta[k].kind != tb[k].kind) {
*handled = true;
return ta[k].kind < tb[k].kind ? -1 : 1;
}A name with no recognized ordinal tokenizes to a single
The same applies to the Suggested rule: take over only when the first difference is a pair of same-kind ordinal tokens with different values, and otherwise leave for (int k = 0; k < n; k++) {
if (ta[k].kind != tb[k].kind) return 0; // different systems -> native
if (ta[k].kind != K_TEXT) {
if (ta[k].val != tb[k].val) {
*handled = true;
return ta[k].val < tb[k].val ? -1 : 1;
}
continue;
}
if (segCmp(ctx, a + ta[k].off, ta[k].len, b + tb[k].off, tb[k].len) != 0)
return 0; // text differs -> native
}
return 0; // including na != nb
2. Hooking Your own setting description already says 3. The TLS index is leaked on unload. thread_local bool g_inHook;
static bool GuardEnter() {
if (g_inHook) return false;
g_inHook = true;
return true;
}
static void GuardLeave() { g_inHook = false; }That also removes the 4. Please add a screenshot to the README. The whole point of the mod is a visible change in file ordering, and a before/after pair makes it immediately understandable — see how classic-this-pc-sort-order.wh.cpp does it (only Optional improvements
Minor polish — none of this affects users, so it's your call.
Functionality notes
Non-critical observations and ideas about the feature behavior itself.
Next steps:
See the review process for details. |
Hi! This PR introduces Chinese Character Natural Sort, a new mod that fixes a long-standing frustration for Chinese Windows users.
The Problem:
By default, Windows sorts Chinese characters alphabetically by their Pinyin pronunciation. This completely breaks sequential file numbering (e.g., under Pinyin rules,
二(2/Er) <三(3/San) <一(1/Yi), resulting in chaotic file orders).The Solution:
This mod hooks
CompareStringEx,CompareStringW, andStrCmpLogicalWto introduce a custom logical sorting algorithm. It intelligently parses Chinese numerals (including complex multi-digit numbers and uppercase variants like 壹/拾/佰), Heavenly Stems, and traditional volume indicators (上/中/下).Performance & Safety:
Since it uses
@include *for system-wide support (Explorer, Open/Save dialogs, etc.), I added a strict performance bypass (if (c < 0x3000) return CAT_NONE;). This ensures that standard ASCII/English strings are instantly passed back to the native Windows API, resulting in zero measurable performance overhead for non-Chinese text.Thanks for reviewing!
Mod authorship
If this pull request introduces a new mod, please complete the section below.
This mod was created by:
Please select the options that best apply. Your selection does not affect the acceptance criteria, but it helps reviewers understand the context of the code and provide relevant feedback.