Skip to content

Commit 0cd724a

Browse files
fix: compare() 未处理多音调汉字的正确排序权重(如“暗”错排在“安”之前),建议改用 STYLE_TONE2 进行底层比较 (#420)
* Update pinyin comparison to use TONE2 style * Update packages/pinyin/src/PinyinBase.ts Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> * Refactor compare method for pinyin comparison Refactor compare method to use arrays instead of joined strings for pinyin comparison. * Add test for sorting Chinese characters with tones * Convert pinyinA and pinyinB to strings for comparison --------- Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
1 parent 6e9354d commit 0cd724a

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

packages/pinyin/src/PinyinBase.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -276,8 +276,8 @@ export default class PinyinBase {
276276
* @return {Number} 返回 -1,0,或 1。
277277
*/
278278
compare(hanA: string, hanB: string): number {
279-
const pinyinA = this.pinyin(hanA);
280-
const pinyinB = this.pinyin(hanB);
279+
const pinyinA = this.pinyin(hanA, { style: ENUM_PINYIN_STYLE.TONE2 });
280+
const pinyinB = this.pinyin(hanB, { style: ENUM_PINYIN_STYLE.TONE2 });
281281
return String(pinyinA).localeCompare(String(pinyinB));
282282
}
283283

packages/pinyin/test/test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,12 @@ describe("pinyin.compare", function() {
367367
const sortedData = data.sort(compare);
368368
expect(sortedData).toEqual("排我序要".split(""));
369369
});
370+
371+
it("同音节不同声调排序", function() {
372+
const data = "马骂妈麻".split("");
373+
const sortedData = data.sort(compare);
374+
expect(sortedData).toEqual("妈麻马骂".split(""));
375+
});
370376
});
371377

372378
describe("pinyin group", function() {

0 commit comments

Comments
 (0)