I am going to talk about how to use the sort method of Array. I would like to comment on the sortBy method _ javascript Technique in protype. js.

Source: Internet
Author: User
Let's take a closer look at how to use the sort method of Array. protype. the sortBy method Array in js. prototype. the 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 ("row by third column now" + 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 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 his code.

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 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!
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.