Usage of sort

Source: Internet
Author: User

A long time has not understood the use of the sort, in the early days is finally the understanding of the study, so to share, if you are also troubled by this method, do not understand the principle, you can look at this article, hope to help.

First, the simplest sort, the purely numeric sort:

var arr=[1,3,5,8,6,4,3];

Arr.sort (function (b) {

return a-B;

})

Here, a will pass in the No. 0 to length-1 element, B will pass in the length of the first to length, before and after the contrast.

Second, the array object contains a sort of numbers:

var data=[{num:111},{num:555},{num:333},{num:444}];

Here, the array contains objects that are sorted according to the size of num within the object:

Data.sort (function (b) {

return a.num-b.num;

})

Here a means that 0--length-1 's object B represents each object of the 1--length because it is sorted according to the data in num, so it needs to return NUM's data, and sort sorts the order of objects of A and b according to the size of Num.

The third type is based on the data in the DOM node, such as the data in one of the columns in the table.

For example, I want to sort the table list according to the first column, here I use JQ to demonstrate:

var tr=$ (' tr ');

Tr.sort (function (b) {
var aa=a.children ("TD"). EQ (0);

var bb=b.children ("TD"). EQ (0);

return AA-BB;

})

The A and B here are the jquery objects that do not have a TR, and the sort will be judged and sorted according to the data you return.

If this article helps you come, order a praise, if there is a better idea, hope to communicate with me, my mailbox is [email protected].

Usage of sort

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.