Go deep and talk about how the array's Sort method is used. Detailed reviews SortBy method _javascript techniques in Protype.js

Source: Internet
Author: User
Tags data structures
The Array.prototype.sort method is an array of sorts that takes a function argument to specify the rules for sorting.

Let's take a look at the simple application of sort:


var arr=[2,1,3,4];
Alert (Arr.sort ())//[1,2,3,4] from small to large arrangement

Now from large to small arrange to get [4,3,2,1]
Alert (Arr.sort function (left,right) {return left>right?-1:1})

Here, the Sort method uses the return value of the parameter function of 1 or-one to determine whether the row or the inverted row

Remember the way I talked to you about using the Function.apply method to get the largest element in an array?
In this paper, two different methods are used to get the maximum value in the array.
Now the sort is ready to show.

var arr=[2,1,3,4];
var minvalue=arr.sort () [0];
var maxvalue=arr.sort () [arr.length-1]//Arr.sort (). Pop ()

How, this is also an alternative implementation method, do not have to write circular traversal.
However, I must point out that this method is the least efficient, and you can still use this technique for arrays of dozens of Bailai elements.
However, if the array is large, the sort () method can be slow enough to make you want to smoke.

Further discussion of sort on the sorting of complex data structures.
1. Sorting multidimensional arrays


var arr=[
[2,1,55,4],
[5,3,22,3],
[1,2,77,2],
[9,4,33,5],
];
Alert ("Default by First row \ n" +arr.sort (). Join ("\ n"))
Alert ("Now in third column \ n" +arr.sort (function (left,right) {return left[2]>right[2]?1:-1}). Join ("\ n"))
Alert (now in the third column is inverted \ n) +arr.sort (function (left,right) {return left[2]>right[2]?-1:1}). Join ("\ n"))
2. Sequencing of 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 Name

var arr=[
{Name: "Bill", money:500},
{name: ' Go_rush ', money:400},
{name: ' Dudu ', money:9000}
];
In turn, it appears that Dudu,bill,go_rush Dudu is the richest, and I am the poorest.
Arr.sort (function (left,right) {return left.money>right.money?-1:1}). each (ShowName)

3. The sort of table I talked to you about yesterday.
See:
Http://www.cnblogs.com/ashun/archive/2006/11/30/appendChild_table_sort.html

More complex table sorting (also with the sort function of array):
http://community.csdn.net/expert/Topicview2.asp?id=5174915


4. There is a very ingenious extension of the sort in protype.js, 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 a function to be passed in, and each element of the array is executed as a parameter, and the result of the function returned is sorted.
Let me break down this function of his.
The Collect method is actually the map method. Equal to
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, now
arr=[2,1,4,3]
Iterator=function (x) {return x*x}

1-3 lines of code get an array like this
[
{Value:2,criteria:4},
{value:1,criteria:1},
{value:4,criteria:16},
{Value:3,criteria:9}
]

4-6 lines of code are sorted according to the criteria: from small to large. Get the end of the line
[
{value:1,criteria:1},
{Value:2,criteria:4},
{Value:3,criteria:9},
{value:4,criteria:16}
]

The 7th line of code is the simplest, take the value attribute of each element, and finally get [1,2,3,4] implementation of the ARR sortby (function ...) Sort


Maybe my language ability is limited, say prototype.js sortby when I do not know how to express in words as well.
I have been so hard to read the interpretation code, I am sorry!

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.