Test.sort (Comparefunc);//Sort function custom sort, for irregular, number, string set//* Comparison function//* @param {Object} param1 parameter to compare 1//* @ param {Object} param2 the parameter to compare 2//* @return {number} if param1 > Param2 returns 1//* if param1 = par AM2 return 0//* if param1<param2returns -1//*/function Comparefunc (PARAM1,PARAM2) {//If the two 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 the two parameters are numeric if (typeof param1== "number" && typeof param2== "Number") {if (param1>param2) return 1; if (param1 = = param2) return 0; if (param1<param2) return-1; }} var Testarray= ["Beijing", "Shanghai", "Dalian", "Chengdu");//Localecompare () method, sort by string, ascending, English, Chinese according to the first character Pinyin document.write (Testarray.sort (function comparefunctio N (param1,param2) {return param1.localecompare (param2); Output: Beijing, Chengdu, Dalian, Shanghai});
About string sorting, sorting by numbers