Work encountered JS array sorting problem, the array is stored in the object, so Baidu, the use of other people's code to modify, the final completion can be reversed, reverse order, can be multi-dimensional sorting function
The source code is as follows:
/** * JS Array sort supports numbers and strings * @param params* @param arrobj obj Required Array Object * @param keyName string Required property name to sort * @param type int optional Default type:0 Zheng Shun type:1 */function arritemsort (arrobj,keyname,type) {//here if it is directly equal to arrobj, equivalent to just a reference to the object, changing the sort will affect the sorting of the original object at the same time. by Arrobj.slice (0),//means the object is copied to another object, the two do not affect var temparrobj = arrobj.slice (0); var compare = function (keyname,type) {return function (Obj1, obj2) {var val1 = Obj1[keyname]; var val2 = Obj2[keyname]; if (!isnan (number (VAL1)) &&!isnan (number (VAL2))) {val1 = number (VAL1); Val2 = number (VAL2); }//If the value is empty, put in the last if (val1 = = NULL && Val2 = = null) {return 0; } else if (Val1 = = null && val2!= null) {return (type==1 -1:1); } else if (Val2 = = null && val1!= null) {return (type==1 1:-1); }//Sort if(Val1 < Val2) {return (type==1 1:-1); } else if (Val1 > Val2) {return (type==1 -1:1);; } else {return 0; }}} return Temparrobj.sort (compare (Keyname,type));}
Examples of Use:
var temp = [ {"name": "ZJF", "Score": "Age": ten}, {"name": "Lyy", "Score": All, "Age": 5}, {"name": "Zzx", " Score ": All," age ": 12}];//according to the age sort var temp1 = Arritemsort (temp," age ", 1); Console.log (TEMP1);//Sort by ages, then by fractions var Temp2 = Arritemsort (Arritemsort (temp, "age", 1), "Score", 1); Console.log (TEMP2);
Source: jsfun.cn
JS array sorting, support for reverse order and multi-dimension sorting