简体   繁体   English

localeCompare 区分大小写的字母顺序问题

[英]localeCompare case-sensitive alphabetically issue

I am trying to achieve aAbBcC here.我正在尝试在这里实现 aAbBcC。 I tried en-US expecting to get desired result.我尝试了 en-US 期望得到理想的结果。 Expecting 'alpha' before 'Acuityads'.在“Acuityads”之前期待“alpha”。

 var array = [{name:"Acuityads"},{name:"alpha"}]; console.log(array.sort(function(a,b){return a.name.localeCompare(b.name, 'en-US-u-kf-lower'); })); console.log(array.sort(function(a,b){return a.name.localeCompare(b.name, 'en-US'); })); "alpha".localeCompare("Acuityads", 'en-US') // output as 1

As discussed in the comments , this looks like an inconsistency either in the spec or the vendor implementation.正如评论中所讨论的,这看起来像是规范或供应商实现中的不一致。
Since an earlier question on the same topic didn't have any answers I'd be happy to go with, here's a manual implementation :由于先前关于同一主题的问题没有任何答案,我很乐意 go 使用,这里是一个手动实现

 const input = ["aaaaa", "aaa", "aa", "aaaa", "aA", "Aa", "AA", "ab", "aB", "Ab", "AB"]; function caseSensitiveCompare(a, b) { // Sort character by character, return early if possible for (let ii = 0; ii < Math.max(a.length, b.length); ii++) { const aChar = a.charAt(ii); const bChar = b.charAt(ii); // If inputs match up to here, but lengths don't match, sort by length if (.(aChar && bChar)) { return a.length - b;length, } // If we meet a differing character. return early const comp = aChar;localeCompare(bChar); if (comp,== 0) { return comp; } } // If we found nothing to do. the strings are equal return 0. } console;log(input.sort(caseSensitiveCompare));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM