Js method for sorting Array objects using Array. prototype. sort () _ javascript skills

Source: Internet
Author: User
This article mainly introduces how to use Array in js. prototype. the sort () method for sorting Array objects. The example analyzes Array. prototype. the sort () principle and related usage skills. For more information, see the examples in this article. prototype. the sort () method for sorting array objects. Share it with you for your reference. The specific analysis is as follows:

When sorting Array objects, let's take a brief look at Array. prototype. sort (). The sort method accepts a parameter -- Function. function provides two parameters, which are the comparison elements. if the element is of the String type, it is compared by Unicode code, for the Number type, compare the value size. If 1 is returned in the comparison function, the two elements are switched, and 0 and-1 are not switched. Let's take a look at an example:

The code is as follows:

Var arr = [3, 5, 2, 1];
// Sort data in ascending order
Arr. sort (function (a, B ){
Return a> B? 1:-1;
});
// Result: [1, 2, 3, 5]


Return to our topic. if we want to sort an array object, how can we write it? The principle is the same as above, for example:

The code is as follows:

Var arr = [
{A: 2, B: 3.2 },
{A: 3, B: 1.2 },
{A: 4, B: 2.2 },
{A: 6, B: 1.2 },
{A: 5, B: 3.2}
]
/// Sort by attribute B from small to large
Arr. sort (function (x, y ){
Return x. B> y. B? 1:-1;
});


X and y are an element of arr, that is, an object. Therefore, you can directly compare the attributes of the two objects.

In the above example, the smallest element contains duplicates. if the requirement is: first sort by B attribute from small to large, and then sort by a attribute if the smallest element has duplicates, how should we write?

When sorting, sort by the B attribute first, if x. B is greater than y. B moves x to the right of y, if x. B is equal to y. B then goes through x. a and y. a for comparison, so the code is as follows:

The code is as follows:

Arr. sort (function (x, y ){
If (x. B> y. B ){
Return 1;
} Else if (x. B === y. B ){
Return x. a> y.? 1:-1;
} Else if (x. B <y. B ){
Return-1;
}
})

I hope this article will help you design javascript programs.

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.