Home > Web Front-end > JS Tutorial > Using sort combined with localeCompare to implement Chinese sorting example in JS_javascript skills

Using sort combined with localeCompare to implement Chinese sorting example in JS_javascript skills

WBOY
Release: 2016-05-16 16:41:15
Original
1502 people have browsed it

When it comes to table sorting, the first thing to talk about must be the sorting of arrays, because array sorting is the basis of table sorting.

JavaScript provides the sort() method for arrays for table sorting. By default, this method will arrange the arrays in Array in the order of ASCII codes. JavaScript also provides the reverse() method for arrays.

Look at an example:

Copy code The code is as follows:

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();


Haha, 5 is larger than 32. Obviously this is not the result we want. As mentioned just now, the sort() method sorts according to the order of ASCII codes.

In fact, the sort() method also allows a function type parameter, which we can call a comparison function. When the comparison function can receive two parameters, the following is the meaning of the return value of the function:

Copy code The code is as follows:

-1: The first parameter is less than the second parameter
0: The first parameter is equal to the second parameter
1: The first parameter is greater than the second parameter

Copy code The code is as follows:

/**
* Comparison function
* @param {Object} param1 Parameter 1 to be compared
* @param {Object} param2 Parameter 2 to be compared
* @return {Number} If param1 > param2 return 1
                                                                                                                                                                                                                                      If param1 == param2 return 0
                                                                                                                                                                                                                  to                            */
           function compareFunc(param1,param2){
//If both parameters are string types
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 and parameter 2 is a number
If(typeof param1 == "string" && typeof param2 == "number"){
Return 1;
             }
​​​​​​​ //If both parameters are numbers
If(typeof param1 == "number" && typeof param2 == "number"){
If(param1 > param2) return 1;
If(param1 == param2) return 0;
If(param1 < param2) return -1;
             }
         }

When we execute arrayTest.sort(compareFunc) we get the correct result.

At this point, we have to explain the usage of the localeCompare() method. This method is a method of sorting strings. It has only one parameter, which is the string to be compared.

The specific instructions are as follows:

1. If the String object is arranged alphabetically before the string in the parameter, a negative number is returned

2. If the String object is arranged after the string in the parameter in character order, return a positive number
3. If the String object is equal to the string in the parameter, return 0

In addition, the localeCompare() method has another unique feature, which can be reflected in its method signature locale (local, local), which means that its implementation is based on regional characteristics. , if it is in the English system, its implementation may be in ascending order of strings, if in Chinese, its implementation may be in accordance with the pinyin of the first letter.

Haha, this means that even if we involve Chinese characters in the program, our sorting will not go wrong.

Please refer to the following procedures:

Copy code The code is as follows:

var testArray = ["foot","本","之","家"];
           document.write(testArray.sort(
              function compareFunction(param1,param2){
Return Param1.localecompare (Param2); // Output:, home, book, feet
            }
));
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template