Introduction to Js array sorting function sort (), jssort
JavaScript sorts multi-dimensional arrays and object arrays. It uses the native sort () method to sort array elements.
The sort () method is used to sort the elements of an array. Syntax:
ArrayObject. sort (sortby)
The returned value is an array reference. Note that arrays are sorted on the original array without generating copies.
If no parameter is used when this method is called, the elements in the array are sorted alphabetically. More precise, the elements are sorted by character encoding. To achieve this, 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 returns a number indicating the relative sequence of the two values. The comparison function should have two parameters, a and B. The returned values are as follows:
If a is less than B, a should appear before B in the sorted array, then a value smaller than 0 is returned.
If a is equal to B, 0 is returned.
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 sort rule function, which will compare the size of two numbers. In fact, our object array sorting works the same way.
If you do not compare the size of a number, you can:
var myarray=["Apple", "Banana", "Orange"]myarray.sort()
After an array directly calls sort (), the array sorts the elements in the array in alphabetical order. More precise, the elements are sorted by character encoding.
For sorting object arrays, we first write a function that constructs a comparison function:
// The by function accepts a member name string as the parameter // returns a comparison function var by = function (name) that can be used to sort the array of objects containing the member) {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 ");}}}
Array to be sorted:
var employees=[]employees[0]={name:"George", age:32, retiredate:"March 12, 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"}
Directly call the function:
employees.sort(by("age"));
By now, object array sorting is basically implemented. How can we sort multiple key values? That is, sort the age first. If the age is the same, compare the name.
In this case, we can further modify the by function so that it can accept the second parameter. When the primary key value produces a match, the other compare method will be called to determine the upper limit.
// The by function accepts a member name string and an optional secondary comparison function as the parameter // and returns a comparison function that can be used to sort the array of objects containing the Member. // when o when [age] and p [age] are equal, secondary comparison functions are used to determine the upper and lower levels of 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 ')));
Now you can use it with confidence. If you can't understand it, copy the by function to your application and call it directly.