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