The array. Prototype. sort method sorts arrays. This method includes a function parameter to specify the sorting rules.
Let's take a look at the simple application of sort:
VaR arr = [2, 1, 3, 4];
Alert (ARR. Sort () // [1, 2, 3, 4] arranged in ascending order
// Now the result is [,] in ascending order.
Alert (ARR. Sort (function (left, right) {return left> right? }))
// Here, the sort method uses the return value 1 or-1 of the Parameter Function to determine whether to forward or reverse
Do you still remember the function. Apply method I mentioned before to obtain the maximum element in the array?
Two different methods are used to obtain the maximum value in the array.
Now, sort can also show off.
VaR arr = [2, 1, 3, 4];
VaR minvalue = arr. Sort () [0];
VaR maxvalue = arr. Sort () [arr. Length-1] // arr. Sort (). Pop ()
This is also an alternative implementation method. You do not need to write loop traversal.
However, I must point out that the efficiency of this method is the lowest. You can still use this technique for arrays of dozens of hundreds of elements.
However, if the array is large, you can use the sort () method to smoke slowly.
We further discuss how sort sorts complex data structures.
1. Sort multidimensional arrays
VaR arr = [
[2, 1, 55, 4],
[5, 3, 22,3],
[,],
[9,4, 33,5],
];
Alert ("by default, \ n" + arr. Sort (). Join ("\ n "))
Alert ("now by the third column \ n" + arr. Sort (function (left, right) {return left [2]> right [2]? 1:-1}). Join ("\ n "))
Alert ("Inverted \ n by third column now" + arr. Sort (function (left, right) {return left [2]> right [2]? -1:1}). Join ("\ n "))
2. Sort complex data structures
Array. Prototype. Each = function (f) {for (VAR I = 0; I <this. length; I ++) f (this [I], I, this )}
Function showname (item) {alert (item. Name)}; // print the name
VaR arr = [
{Name: "bill", money: 500 },
{Name: "go_rush", money: 400 },
{Name: "Dudu", money: 9000}
];
// Display Dudu, Bill, and go_rush in sequence. Dudu is the richest, and I am the poorest.
Arr. Sort (function (left, right) {return left. Money> right. Money? -1:1}). Each (showname)
3. Sort tables. I talked about this topic yesterday.
See:
Http://www.cnblogs.com/ashun/archive/2006/11/30/appendChild_table_sort.html
More complex table sorting (also using the sort function of array ):
Http://community.csdn.net/expert/Topicview2.asp? Id = 5174915
4. In protype. JS, there is an ingenious extension of sort. first look at hisCode
1 sortby: function (iterator ){
2 return this. Collect (function (value, index ){
3 return {value: value, criteria: iterator (value, index )};
4}). Sort (function (left, right ){
5 var A = left. Criteria, B = right. criteria;
6 return a <B? -1: A> B? 1: 0;
7}). Pluck ('value ');
8 },
This sortby allows you to input a function, execute this function as a parameter for each element of the array, and finally sort the results returned by the function.
Next I will break down his function.
The collect method is actually the map method.
Array. Prototype. Map = function (f ){
For (VAR I = 0; ret = []; I <this. length; I ++) RET [I] = f (this [I], I, this)
Return ret
}
For example
Arr = [2, 1, 4, 3]
Iterator = function (x) {return x * x}
1-3 lines of code will get such an array
[
{Value: 2, criteria: 4 },
{Value: 1, criteria: 1 },
{Value: 4, criteria: 16 },
{Value: 3, criteria: 9}
]
4-6 lines of code sort the array by criteria: from small to large.
[
{Value: 1, criteria: 1 },
{Value: 2, criteria: 4 },
{Value: 3, criteria: 9 },
{Value: 4, criteria: 16}
]
The Code in line 7th is the simplest. Get the value attribute of each element and finally obtain [,] to sort arr by (function ...).
Maybe my language expression skills are limited. When I talk about prototype. js sortby, I just don't know how to express it in text.
I am so sorry for the hard work I have written!