Home  >  Article  >  Web Front-end  >  JavaScript study notes (3) Array sorting and the use of localeCompare() method in Chinese character sorting

JavaScript study notes (3) Array sorting and the use of localeCompare() method in Chinese character sorting

黄舟
黄舟Original
2016-12-19 17:32:371729browse

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.
Take a look at the example:
1 function sortArray(){
2 var arrayTest = ["z",5,2,"a",32,3];
3 arrayTest.sort();
4 alert(arrayTest.to String ()); //output:2,3,32,5,a,z
5 arrayTest.reverse();
6 alert(arrayTest.toString()); //output:z,a,5,32, 3,2
7 }
8 sortArray(); Haha, 5 is larger than 32. Obviously this is not the result we want. As mentioned just now, the sort() method sorts in the order of ASCII codes. In fact, the sort() method also allows a parameter of function type, 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:
-1: the first parameter 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
Look at an example:
1 /**数 2 * Comparison function * 3 * @param {Object} param1 Parameter 1
4 * @param {Object} Param2 Parameters 2
5 *Rturn {Number} If Param1 & GT; Param2 returns 1
6 *If Param1 == Param2 returns 0
7 *If Param1 & LT; Param2 returns -1
8*/
9 function compareFunc(param1 , Param2) {m10 // If both parameters are string types
11 IF (Typeof Param1 == "String" && Typeof Param2 == "String") {
12 RETURN PARAM1.LOCALECOMPARE (Param2);
3 }
14                     // If parameter 1 is a number, parameter 2 is a string
15                          if (typeof param1 == "number" && typeof param2 == " ") {
16                                                                                                                                                    
18                  // If parameter 1 is a string and parameter 2 is a number
19     if(typeof param1 == "string" && typeof param2 == "number"){
20        return 1;
21     }
22   for Digital f 23 IF (Typeof Param1 == "Number" && Typeof Param2 == "Number") {
24 if (Param1 & GT; Param2) Return 1;
25 IF (Param1 == Param2) Return 0;
26 If (if (if (if param1 < param2) return -1;
27}
28} When we execute arrayTest.sort (comparefunc), we get the right 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 in alphabetical order before the string in the parameter, a negative number is returned
2. If the String object is arranged in alphabetical order after the string in the parameter, a positive number is returned
3. If String The object is equal to the string in the parameter and returns 0
In addition, the localeCompare() method has another unique feature, which can be reflected in its method signature locale (local, local), that is to say, his The implementation is based on regional characteristics. If it is in the English system, its implementation may be in ascending order of strings. If it is 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.
Refer to the following procedure:
1 var testArray = ["Zheng", "State", "Xin", "Source", "Xin", "Information", "Technology", "Technology", "Share", "Share" , "Yes", "Limited", "Gong", "Division"];
2 Document.write (TestArray.sort output: portal, public, stock, technical, technique, division, division, restriction, faith, faith, yes, source, Zheng, state
5}
6)); ) method, please pay attention to the PHP Chinese website (www.php.cn) for more related content!

Statement:
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