When it comes to table sorting, the first thing we need to talk about is array sorting, because array sorting is the basis of table sorting.
JavaScript provides a 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 an array reverse() method for arrays.
Take a look at the example:
The code copy 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 bigger than 32, it is obvious that this is not the result we want. I have just said that the sort() method is sorted in the order of ASCII code.
In fact, the sort() method also allows to take a function-type parameter, which we can call a comparison function. When the comparison function can receive two parameters, the following function returns the meaning of the value:
The code copy 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
The code copy is as follows:
/**
* Comparison function
* @param {Object} param1 Param 1 to compare
* @param {Object} param2 Param 2 to compare
* @return {Number} If param1 > param2 returns 1
* If param1 == param2 returns 0
* If param1 < param2 returns -1
*/
function compareFunc(param1,param2){
//If both parameters are 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 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, which is a method to sort strings, with only one parameter, namely the string to be compared.
The specific explanation is as follows:
1. If the String object is arranged alphabetically before the string in the parameter, return a negative number
2. If the String object is arranged in the character order after the string in the parameter, 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. This unique feature can be reflected in its method signature locale (on-site, local), that is, its implementation is based on regional characteristics. If in the English system, its implementation may be in ascending order of strings, and if in Chinese, its implementation is based on 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 program:
The code copy is as follows:
var testArray = ["foot","book","ite","home"];
document.write(testArray.sort(
function compareFunction(param1,param2){
return param1.localeCompare(param2); //output:一, home, book, foot
}
));