javascript sort with unicode

前端 未结 3 1514
野的像风
野的像风 2020-12-03 03:04

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:

function sortComparer(         


        
相关标签:
3条回答
  • 2020-12-03 03:25

    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).

    Test Online

    Good Luck

    0 讨论(0)
  • 2020-12-03 03:35

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

    function sortComparer(a,b){
        return a.title.localeCompare(b.title)
    };
    
    0 讨论(0)
  • 2020-12-03 03:38

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

    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

    0 讨论(0)
提交回复
热议问题