Array of JavaScript objects sorted (by account order)

Source: Internet
Author: User

Demand

Data obtained from the background this is what God wants.

Background

Gets the tabular data from the background and populates it with Excel. Of course, it is filled with JS. The array of JS itself has the sort () function. But for a column of data. Here are two columns of data. Do you have a swollen handle? See the world of Arrayobject.sort (SortBy);
SortBy is empty by default. That is, sort by the encoding of the string.
What is the role of SortBy?
If you want to sort by other criteria, you need to provide a comparison function that compares two values and returns a number that describes the relative order of the two values. The comparison function should have two parameters A and B with the following return values:
If a is less than B, a value that is less than 0 is returned if a should appear before B in the sorted array.
If a equals B, 0 is returned.
If a is greater than B, a value greater than 0 is returned
Well, that's a good run.

Ideas

Save the result set as an array of JS objects. The object array is then sorted.

varData=[];varresult=collection of results obtained from the background for(varI=0;i<result. rowcount;i++) {Data[i]={n:result.getvalue (i, "D"), B:result.getvalue (i, "E"))}}functionCompare (PropertyName) {return function(Object1, object2) {varValue1 =Object1[propertyname]; varvalue2 =Object2[propertyname]; if(Value2 >value1) {            return-1; } Else if(Value2 <value1) {            return1; } Else {            return0; }}}data.sort (Compare (N) ; output Excel .....
Understand

JS has a sort method, optional parameters. The parameter is a comparison function. The comparison function has two parameters, two parameters are used to compare, greater than, less than, equals. return -1,0,1 respectively

This allows the array to invoke the contrast function.

Array of JavaScript objects sorted (by account order)

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.