JS USES sort in conjunction with localeCompare to implement Chinese sort instance

  • 2020-03-30 03:36:13
  • OfStack

When it comes to table sort, the first thing you have to say is array sort, because array sort is the basis of table sort.

JavaScript provides a sort() method for arrays for sorting tables, which by default causes arrays in an Array to be sorted in ASCII order, and JavaScript provides a method for arrays to be sorted in reverse().

Take a look at the example:


 function sortArray(){
             var arrayTest = ["z",5,2,"a",32,3];
             arrayTest.sort();
             alert(arrayTest.toString());     //output:2,3,32,5,a,z
             arrayTest.reverse();
             alert(arrayTest.toString());    //output:z,a,5,32,3,2
         }
         sortArray();


Well, 5 is bigger than 32, which is clearly not what we want, as we already said sort() is sorted in ASCII order.

In fact, the sort() method also allows an argument of a function type, which we can call a comparison function. When the comparison function can receive two arguments, the function returns the value of:


-1 : first parameter Less than Second parameter
0 : first parameter Is equal to the Second parameter
1 : first parameter Is greater than Second parameter


/**
  * The comparison function
  * @param {Object} param1 Parameters to compare 1
           * @param {Object} param2 Parameters to compare 2
           * @return {Number} if param1 > param2 return 1
           *                     if param1 == param2 return 0
           *                     if param1 < param2 return -1
           */
          function compareFunc(param1,param2){
             //If both arguments are of string type
             if(typeof param1 == "string" && typeof param2 == "string"){
                 return param1.localeCompare(param2);
             }
             //If parameter 1 is a number, parameter 2 is a string
             if(typeof param1 == "number" && typeof param2 == "string"){
                 return -1;
             }
             //If parameter 1 is a string, parameter 2 is a number
             if(typeof param1 == "string" && typeof param2 == "number"){
                 return 1;
             }
             //If both arguments are Numbers
             if(typeof param1 == "number" && typeof param2 == "number"){
                 if(param1 > param2) return 1;
                 if(param1 == param2) return 0;
                 if(param1 < param2) return -1;
             }
         }

We get the correct result when we execute arraytest.sort (compareFunc).
At this point, we have to explain the use of the localeCompare() method, which is a method that sorts strings with only one argument: the string to compare.

The details are as follows:

1. If the String object is placed alphabetically before the String in the argument, it returns a negative number
2. If the String object follows the String in the argument in character order, it returns a positive number
Returns 0 if the String object is equal to the String in the argument

In addition, localeCompare () method is a unique, the uniqueness in the method signature locale (site, local), that is to say his implementation according to the regional features, if in English system, he may be in accordance with the string of ascending, if in Chinese, he is in accordance with the first letter of the alphabet.


Related articles: