The sort function of the array today can accept custom comparison functions, which is a wonderful thing.
This makes it extremely easy to sort numbers that are within the range of a numeric type.
var myarray = [ -80,-13.888,-10,-9.6,-9.4,-1,2,2,3,5.823,7.999,20,0,22,55,312];
Ijs.put (Myarray.sort (function (a,b) {return a-b}));
Debugging information:
-80,-13.888,-10,-9.6,-9.4,-1,0,2,2,3,5.823,7.999,20,22,55,312
If you want to be descending, a-b change to B-a:
var myarray = [ -80,-13.888,-10,-9.6,-9.4,-1,2,2,3,5.823,0,7.999,20,22,55,312];
Ijs.put (Myarray.sort (function (a,b) {return b-a});//Here is B-a
Debugging information:
312,55,22,20,7.999,5.823,3,2,2,0,-1,-9.4,-9.6,-10,-13.888,-80
A function that returns a comparison function is constructed to achieve more flexible purposes. For this I wrote a one-dimensional two-dimensional array of general sorting functions as follows (without adequate testing, such as for production, it is best to test again AH):
The same type is the same as the size, the fluctuation is specified by the first parameter of option//type different than the type, the elevation is specified by the second parameter of option//If an object or an array, the property or element is specified by name to participate in the sort///the same size result is the same
y Specifies the rule sort var by = function (Name,option,secondby) {return function (o,p) {var a,b; var sign=1;//adjust order var r;//save return value//If an object or array, specify the property or element to participate in the sort by name if (o && typeo
F o = = = ' object ') {a = O[name];
}else{a = O;
} if (P && typeof p = = = ' object ') {b = p[name];
}else{B = p;
} if (typeof a = = typeof b) {//Lift by option's first parameter specify switch (Option[0]) {
Case "desc": sign = 1;
Break
Case "ASC": sign =-1;
Break Default:sign = 1;//Default}}else{//type is different than type a = typeof A;
b = typeof B; The lift is specified by the second parameter of option switch (Option[1]) {case "desc": Sign
= 1;
Break
Case "ASC": sign =-1;
Break
Default:sign = 1;//Default}} if (a = = b) { Recursive support for multiple parameter ordering (20121231 supplements: If the pass is not by is not recursive) R = typeof Secondby = = = "function"?
Secondby (o,p): 0;
}else if (a<b) {r = 1*sign;
}else{r = -1*sign;
} return R; }//Determine if array var is_array = function (value) {return value &&//typeof value = = = = "Object" &&//typeof to detect type Object
typeof Value.length = = = "Number" &&//length property is number type typeof Value.splice = = "function" &&//There is a splice method! (Value.propertyisenumerable ("Length"))//length is an not-enumerated property}//Output two-dimensional array
http://www.bianceng.cn var puts = function (myobj) {for (Var i=0;i<myobj.length;i+=1) {var
TMP = ""; if (Is_array (Myobj[i])) {for (var j=0;j<myobj[i].length;j+=1) {tmp = tmp + myobj[i]
[j] + ","; }}else{for (name in Myobj[i]) {TMP = tmp + name + ': ' + myobj[i][name]
+ ",";
} ijs.put (TMP);
} var myarray=new Array (); Example 1 myarray = [{"A": 1, "B": 1, "C": 1}, {"A": 3, "B": 3, "C": 1}, {"A": 3, "B": 4, "C": 1}, {"A": 3
, "B": 1, "C": 1}, {"A": 2, "B": 1, "C": 2}, {"A": 2, "B": 1, "C": 1},];
MyArray = Myarray.sort (by ("A", ["ASC"], by ("B", [], by ("C", ["ASC"]));
Puts (myarray); Example 2 myarray = [[1,1,1], [3,3,1], [3,4,1], [3,1,1], [2,1,2], [2,1,1]];
MyArray = Myarray.sort (by (0,["ASC"], by (1,[), by (2,["ASC")));
Puts (myarray);
Example 3 myarray = [ -80,-13.888,-10,-9.6,-9.4,-1,2,2,3,5.823,0,7.999,20,22,55,312];
MyArray = Myarray.sort (by (null,["ASC"]);
Ijs.put (""); Ijs.put (myarray);