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 the 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. toString()); //output:2,3,32,5,a,z
5 arrayTest.reverse();
6 alert(arrayTest.toString()); //output:z,a,5,32 ,3,2
7 sortArray(); . 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
1: The first parameter is greater than the second parameter
Look at an example:
& 1/**
2 * Comparison function
3 * @param {Object} param1 Parameter to be compared 1
4 * @param {Object} param2 Parameter to be compared 2
5 > param2 returns 1
6 *If Param1 == Param2 returns 0
7*If Param1 & LT; Param2 returns -1
8*/
9 Function Comparefunc (Param1, Param2) {
10 // If both parameters are string types
11 if (typeof param1 == "string" && typeof param2 == " string"){
12 string") == "number" && typeof param2 == "string "){
16 return -1;
17 param2 == "number"){
20 return 1;
21 return if(param1 > param2) return 1; Ar25 if (Param1 == Param2) Return 0;
26 IF (Param1 & LT; Param2) Return -1; .
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 a 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.
Please refer to the following procedures:
1 var testArray = ["Zheng", "zhou", "xin", "source", "xin", "information", "technology", "skills", "shares", "shares", "you", "Limited", "public", "company" ];
2 document.write(testArray.sort(
3 function compareFunction(param1,param2){
4 return param1.localeCompare(param2); //output: copies , public ,stock,technology,technical,information,limit,faith,faith,source,Zheng,zhou
5 }
6 ));