When it comes to table sorting, the first thing we should talk about is array sorting, Because array sorting is the basis of table sorting. JavaScript provides the sort () method for Arrays for table sorting. By default, this method will sort the arrays in Array in the order of ASCII codes, javascript also provides array reverse-order reverse () for arrays (). When it comes to table sorting, the first thing we should talk about is array sorting, Because array sorting is the basis of table sorting. JavaScript provides the sort () method for Arrays for table sorting. By default, this method will sort the arrays in Array in the order of ASCII codes, javascript also provides array reverse-order reverse () 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. toString (); // 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 bigger than 32, obviously this is not the result we want. We have already said that the sort () method is sorted by ASCII code. In fact, the sort () method also allows a parameter of the function type, which can be called a comparison function. When the comparison function can receive two parameters, the meaning of the return value of the function is as follows:
-1: The first parameter is smaller than the second parameter.
0: the first parameter is equal to the second parameter.
1: The first parameter is greater than the second parameter
Let's look at an example:
1 /**
2 * comparison functions
3 * @ param {Object} param1 parameter 1 to be compared
4 * @ param {Object} param2 parameter 2 to be compared
5 * @ return {Number} If param1> param2 returns 1
6 * If param1 = param2, 0 is returned.
7 * If param1 <param2 returns-1
8 */
9 function compareFunc (param1, param2 ){
10 // if both parameters are strings
11 if (typeof param1 = "string" & typeof param2 = "string "){
12 return param1.localeCompare (param2 );
13}
14 // If parameter 1 is a number and parameter 2 is a string
15 if (typeof param1 = "number" & typeof param2 = "string "){
16 return-1;
17}
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 // if both parameters are numbers
23 if (typeof param1 = "number" & typeof param2 = "number "){
24 if (param1> param2) return 1;
25 if (param1 = param2) return 0;
26 if (param1 <param2) return-1;
27}
28} when we run arrayTest. sort (compareFunc), we get the correct result.
Here, we have to explain the usage of the localeCompare () method. This method sorts strings and has only one parameter, that is, the string to be compared. The details are as follows:
1. If the String object is placed before the String in the parameter in alphabetical order, a negative number is returned.
2. If the String object is placed after the String in the parameter in the character order, a positive number is returned.
3. If the String object is equal to the String in the parameter, 0 is returned.
In addition, the localeCompare () method has its own uniqueness, which can be reflected in its method signature locale (on-site, local, that is to say, his implementation follows the regional characteristics. In the English system, his implementation may follow the string ascending order. If he is in Chinese, its implementation is based on the first letter of pinyin. That is to say, even if we involve Chinese characters in the program, we will not return errors in sorting.
Refer to the following procedure:
1 var testArray = ["zheng", "Zhou", "Xin", "Source", "Xin", "Xi", "tech", "Shu ", "shares", "shares", "yes", "Limited", "public", "division"];
2 document. write (testArray. sort (
3 function compareFunction (param1, param2 ){
4 return param1.localeCompare (param2); // output: Copy, public, stock, technology, technology, division, interest, limit, letter, letter, yes, source, Zheng, state
5}
6 ));
The above is the content used by the localeCompare () method in array sorting and Chinese Character sorting. For more information, see PHP Chinese website (www.php1.cn )!