Osn ARRAY.SORRT

Source: Internet
Author: User

The Array.prototype.sort method is to sort an array, which takes a function parameter that specifies the collation of the order.

The default sort function of JS is to treat all elements to be sorted as "strings", followed by sorting;

1 more simple one-dimensional arrays:

var arr=[2,1,3,4];

alert (Arr.sort ())//[1,2,3,4] arranged from small to large


2. Sorting a multidimensional array:

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 column three" +arr.sort (function (left,right) {return left[2]>right[2]?1:-1}). Join ("\ n"))

Alert ("Now reverse the third column \ n" +arr.sort (function (left,right) {return left[2]>right[2]?-1:1}). Join ("\ n"))


3. Sorting 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}

];

Arr.sort (function (left,right) {return left.money>right.money?-1:1}). each (showname);


As mentioned above, the default sort function is to treat all the elements to be sorted as "strings" and then sort them, so you can't be smart to detect them before comparing them, so you often get a more outrageous result;

Fortunately, we can customize the comparison function to replace the JS default comparison function, this custom function should accept two parameters (A, b), and if the two parameters return "0", if a in front of B, a negative number is returned;

n = [4,8,15,16,25,42];

N.sort (function (A, B)

{

return a-B;

});

The above function can make the number array, the order is correct, the single cannot be a string ordering, if we want to give any containing simple numeric (number, String) array order:

Here is a more practical way:


var m = ["AA", "BB", "a", 4,8,16,19,23,42];

M.sort (function (A, B)

{

if (a = = b)

{

return 0;

}

if (typeof a = = = typeof b)

{

Return a < b? -1:1;

}

Return typeof a < typeof B? -1:1;

});

From this, we can also sort the array of objects, and of course, when sorting, the content of the comparison can make the same properties of all objects;

The following is an algorithm for sorting an identical property of a group of objects:


var sortobj = [{firstName: "Joe", LastName: "Howard"},

{firstName: "Tom", LastName: "Fine"},

{firstName: "Blue", LastName: "Besser"},

{firstName: "Jhon", LastName: "Howard"},

{firstName: "Jhon", LastName: "Obam"},

{firstName: "Farmer", LastName: "DeRita"}];

var sortby = 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 {name: ' Error ', message: ' Expected an object when sorting by ' + name};

}

};

}

Because the sort method is unstable, this is called:

Sortobj.sort (SortBy ("FirstName")). Sort (SortBy ("LastName"));

There is no guarantee that you will produce the results you want, but if you want to sort based on multiple key values, you need to optimize the algorithm above,

Let it take two parameters, and when the primary key value produces a matching time, another compare method will call a showdown:

When O[name] and p[name] are equal, the secondary comparison function is used for a showdown, with the following specific algorithm:


var sortby = function (Name,minor)

{

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 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

{

Throw {name: ' Error ', message: ' Expected an object when sorting by ' + name};

}

};

}

Array of sort or relatively powerful and easy to use, the above are personal remarks, welcome advice, good, their own nonsense to this end;

Thank you.


This article is from the "7439523" blog, please be sure to keep this source http://7449523.blog.51cto.com/7439523/1590830

Osn ARRAY.SORRT

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.