[JavaScript] sorting of object arrays

Source: Internet
Author: User

[JavaScript] sorting of object arrays

 

The sort method of the array sorting function in Javascript. By default, the sorting is performed in ascending order of ASCII characters.
Arrayobj. Sort (sortfunction );
Parameter: sortfunction
Optional. Is the name of the function used to determine the order of elements. If this parameter is omitted, the elements are listed in ascending order of ASCII characters.
The sort method sorts array objects appropriately. During execution, no new array objects are created.
If a function is provided for the sortfunction parameter, the function must return one of the following values:
Negative value if the first parameter is smaller than the second parameter.
Zero. If the two parameters are equal.
If the first parameter is greater than the second parameter.
The preceding method is very convenient in one-dimensional sorting, but how does one sort multiple key values like order by in SQL statements?
Multi-dimensional array multi-key value sorting requires complexity, but does not need to be solved cyclically. The actual solution is the same.
Number:
In the following example, the multi-dimensional array of numbers is sorted in the order of 5th columns, 9th columns, and 3rd columns, such as order by col5, col9, col7 in SQL statements. You can subtract two items directly, and use the result as the return value.
<Script language = JavaScript>
VaR myarray = new array ();
For (VAR I = 0; I <10; I ++ ){
Myarray [I] = new array ();
Myarray [I] [0] = math. Floor (math. Random () * 10 );
Myarray [I] [1] = math. Floor (math. Random () * 10 );
Myarray [I] [2] = math. Floor (math. Random () * 10 );
Myarray [I] [3] = math. Floor (math. Random () * 10 );
Myarray [I] [4] = math. Floor (math. Random () * 10 );
Myarray [I] [5] = math. Floor (math. Random () * 10 );
Myarray [I] [6] = math. Floor (math. Random () * 10 );
Myarray [I] [7] = math. Floor (math. Random () * 10 );
Myarray [I] [8] = math. Floor (math. Random () * 10 );
}
  
Myarray. Sort (

Function (x, y ){

If (X [4]! = Y [4]) {

Return X [4]-y [4];

} Else if (X [8]! = Y [8]) {

Return X [8]-y [8];

} Else if (X [6]! = Y [6]) {

Return X [6]-y [6];

} Else {

Return 1;

}

 

}

);


For (VAR I = 0; I <myarray. length; I ++ )...{
Document. Write (myarray [I]. Join (",") + "<br/> ");
}
</SCRIPT>
Character:
The project in sortfunction cannot be directly subtracted like a number.
Str1.localecompare (str2) method for comparison to meet the returned value. The following table lists the sorting of Column 1 and column 2 of multi-dimensional arrays.
Function sortfunction (array ){
Return array. Sort (function (x, y )...{
Return (X [0] = Y [0])? (X [1]. localecompare (Y [1]) :( X [0]. localecompare (Y [0])
});
}
Therefore, the sorting function of arrayobject. Sort (sortfunction) is very powerful, and the order by function in SQL statements can be achieved at last.
This article is taken from the IT learner http://www.itlearner.com/article/2008/4003.shtml

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.