javascript 使用 unicode 进行排序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6909126/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 22:17:57  来源:igfitidea点击:

javascript sort with unicode

javascriptsorting

提问by Andrej Kaurin

There are a lot of examples for sorting some JSON array by some property (i.e. 'title') We are using compare function like this one:

有很多示例可以按某些属性(即“标题”)对一些 JSON 数组进行排序,我们正在使用这样的比较函数:

function sortComparer(a, b) {
        if (a.title == b.title)
            return 0;
        return a1 > b1 ? 1 : -1;
    }

Problem is that Serbian Latin alphabet order looks like "A, B, C, ?, ?, D,..." When using sortComparer above I am getting D sorted before "?" or "?". Any idea how to sort respecting current culture language?

问题是塞尔维亚拉丁字母顺序看起来像“A,B,C,?,?,D,...” 当使用上面的 sortComparer 时,我将 D 排在“?”之前。或者 ”?”。任何想法如何排序尊重当前的文化语言?

回答by Andris

If the locale in your system is set correctly then you can use localeComparemethod instead of greater-thanoperator to compare the strings - this method is locale aware.

如果您系统中的语言环境设置正确,那么您可以使用localeComparemethod 而不是大于运算符来比较字符串 - 此方法是语言环境感知的。

function sortComparer(a,b){
    return a.title.localeCompare(b.title)
};

回答by Iman Bahrampour

For sorting an array with custom setting do as following:

要使用自定义设置对数组进行排序,请执行以下操作:

  1. Create an array with a custom order of alphabets:

    var alphabets = ["A", "B", "C", "?", "?", "D","D?","?","E","F","G","H","I","J","K","L","Lj","M","N","Nj","O","P","R","S", "??","T","U","V","Z","?"];

  2. Create a list of test array:

    var testArrray = ["B2","D6","A1","?5","?4","C3"];

  3. Create a sort function name:

    function OrderFunc(){
              testArrray.sort(function (a, b) {
                  return CharCompare(a, b, 0);
              });
          }
    
  4. create the CharCompare function(index: sort "AAAB" before "AAAC"):

     function CharCompare(a, b, index) {
      if (index == a.length || index == b.length)
          return 0;
      //toUpperCase: isn't case sensitive
      var aChar = alphabets.indexOf(a.toUpperCase().charAt(index));
      var bChar = alphabets.indexOf(b.toUpperCase().charAt(index));
      if (aChar != bChar)
          return aChar - bChar
      else
          return CharCompare(a,b,index+1)
    

    }

  5. Call OrderFunc for sorting the testArray(the result will be : A1,B2,C3,?4,?5,D6).

  1. 创建一个具有自定义字母顺序的数组:

    var alphabets = ["A", "B", "C", "?", "?", "D","D?","?","E","F","G","H","I","J","K","L","Lj","M","N","Nj","O","P","R","S", "??","T","U","V","Z","?"];

  2. 创建测试数组的列表:

    var testArrray = ["B2","D6","A1","?5","?4","C3"];

  3. 创建排序函数名称:

    function OrderFunc(){
              testArrray.sort(function (a, b) {
                  return CharCompare(a, b, 0);
              });
          }
    
  4. 创建 CharCompare 函数(索引:在“AAAC”之前排序“AAAB”):

     function CharCompare(a, b, index) {
      if (index == a.length || index == b.length)
          return 0;
      //toUpperCase: isn't case sensitive
      var aChar = alphabets.indexOf(a.toUpperCase().charAt(index));
      var bChar = alphabets.indexOf(b.toUpperCase().charAt(index));
      if (aChar != bChar)
          return aChar - bChar
      else
          return CharCompare(a,b,index+1)
    

    }

  5. 调用 OrderFunc 对 testArray 进行排序(结果将是:A1,B2,C3,?4,?5,D6)。

Test Online

在线测试

Good Luck

祝你好运

回答by Dinesh Rabara

Use The Intl.Collator like you will get perfect sorting result

使用 Intl.Collat​​or 就像你会得到完美的排序结果

function letterSort(lang, letters) {
  letters.sort(new Intl.Collator(lang).compare);
  return letters;
}

console.log(letterSort('gu', ['?','?','?']));
// expected output: Array ["a", "?", "z"]

console.log(letterSort('sv', ['a','z','?']));
// expected output: Array ["a", "z", "?"]

More detail you can check here https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Collator

您可以在此处查看更多详细信息 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Collat​​or