JavaScript implements multidimensional array, object array ordering, its practical is the native sort () method, for the elements of the array to sort.
The sort () method is used to sort the elements of an array. The syntax is as follows:
Arrayobject.sort (SortBy)
The value returned is a reference to the array. Note that the array is sorted on the original array and no replicas are generated.
If the method is invoked without parameters, the elements in the array are sorted alphabetically, and more precisely, sorted in the order of character encoding. To do this, you should first convert the elements of the array into strings (if necessary) for comparison.
If you want to sort by other criteria, you need to provide a comparison function that compares two values, and then returns a number that describes the relative order of the two values. The comparison function should have two parameters A and B, and the return value is as follows:
If a is less than B, a value less than 0 is returned in the sorted array before a should appear in B.
If a equals B, it returns 0.
If a is greater than B, a value greater than 0 is returned.
function Numascsort (a,b)
{return
a-b;
}
function Numdescsort (a,b)
{return
b-a;
}
var arr = new Array (3600, 5010, 10100, 801);
Arr.sort (numdescsort);
Alert (arr);
Arr.sort (numascsort);
Alert (arr);
Sort (fun) accepts a collation function that compares the size of 2 digits. And our array of objects, in fact, is the same principle.
If you do not compare the size of a number, you can do this:
var myarray=["Apple", "Banana", "Orange"]
myarray.sort ()
Arrays call sort () directly, the arrays are sorted alphabetically by the elements in the array, and more precisely, sorted in the order of character encoding.
For object array ordering, we first write a function that constructs the comparison function:
The by function takes a member name string as a parameter
//and returns a comparison function that can be used to sort the array of objects that contains the member
var by = function (name) {return
function (O, p) {
var a, b;
if (typeof o = = "Object" && typeof p = = "Object" && o && p) {
a = O[name];
b = P[name];
if (A = = = B) {return
0;
}
if (typeof a = = typeof b) {return
a < b -1:1;
}
Return typeof a < typeof B? -1:1;
}
else {
throw ("Error");}}
The array to sort:
var employees=[]
employees[0]={name: "George", age:32, Retiredate: "March, 2014"}
employees[1]={name: " Edward ", Age:17, Retiredate:" June 2, 2023 "}
employees[2]={name:" Christine ", age:58, Retiredate:" December 20, 2036 " }
employees[3]={name: "Sarah", age:62, Retiredate: "April 30, 2020"}
Call function directly:
Employees.sort (by ("Age"));
Here, the object array sort is basically implemented. So how do you implement multiple key-value sorting? This means that the age is sorted first, and if the age is the same, then name is compared.
At this point, we can further modify the by function so that it can accept the second parameter, and when the primary key value produces a match, another compare method will be invoked to decide the difference.
The by function takes a member-name string and an optional secondary comparison function as a parameter
//and returns a comparison function that can be used to sort the array of objects containing the member
//When O[age] and p[age] are equal, the secondary comparison function is used to
decide var by = function (Name,minor) {return
function (o,p) {
var a,b;
if (o && p && typeof o = = ' object ' && typeof p = = ' object ') {
a = O[name];
b = P[name];
if (A = = = B) {return
typeof minor = = = ' function '? minor (O,p): 0;
}
if (typeof a = = typeof b) {return
a < b -1:1;
}
Return typeof a < typeof B? -1:1;
} else{
thro ("error");
}} Employees.sort (by "Age", by (' name '));
OK, now you can use it with ease. If you do not understand, you can directly copy this by function to your application, direct call can be.