Introduction to sort () sorting instances in javascript

Source: Internet
Author: User

In js, array sorting uses array. sort (). The following is an example of sort () array sorting, hoping to help you.

By default, the sort () method arranges array items in ascending order-that is, the smallest value is located at the beginning, and the maximum value is located at the end. To achieve sorting, the sort () method calls the toString () transformation method of each array item, and then compares the obtained strings to determine how to sort them. Even if each item in the array is a numerical value, the sort () method compares strings as follows:

The Code is as follows: Copy code

Var value = [8, 10, 0, 5, 20];
Value. sort (value );
Alert (value); // 0, 10, 20, 5, 8

It can be seen that the sort () method will change the original sequence based on the test string results even if the sequence of the values in the example is correct. Because the value 5 is smaller than 10, but the value 5 is located before the value 10 during string comparison, the order of the array is modified. Needless to say, this sorting method is not the best solution in many cases. Therefore, the sort () method can receive a comparison function as a parameter, so that we can specify the value before which.
The comparison function receives two parameters. If the first parameter is located before the second parameter, a negative number is returned. If the two parameters are equal, 0 is returned, if the first parameter is placed after the second parameter, a positive number is returned. The following is a simple comparison function:

The Code is as follows: Copy code

<Script type = "text/javascript">

Function compare (value1, value2 ){
If (value1 <value2 ){
Return-1;
} Else if (value1> value2 ){
Return 1;
} Else {
Return 0;
}
}
Function gradeDown (value1, value2 ){
If (value1 <value2 ){
Return 1;
} Else if (value1> value2 ){
Return-1;
} Else {
Return 0;
}
}
Var values = [0, 8, 4, 9, 15];
Var down = [0, 8, 4, 9, 15];
Values. sort (compare );
Down. sort (gradeDown );
Document. write ("ascending:" + values + '<br/> ')
Document. write ("descending order:" + down + '<br/> ');
Var value = [8, 10, 0, 5, 20];
Value. sort ();
Alert (value );
</Script>

For the value type or its valueof () method, the object type of the value type is returned. You can use a simpler comparison function. This function only requires the second value minus the first value.

The Code is as follows: Copy code
Function va (value1, value2 ){
Return value2-value1;
} // Ascending
Function vac (value1, value2 ){
Return value1-value2;
} // Descending order
Var co = [88,22, 44,11, 20]
Co. sort (va)
Var ka = [99,22, 44,88, 23]
Ka. sort (vac)
Document. write (co + '<br> ');
Document. write (ka)

If you just want to reverse the original order of the array, the reveres () method will be faster.

As follows:

The Code is as follows: Copy code


Var arrA = [6, 2, 4, 3, 5, 1];
/** // * ArrA. sort ();
Document. writeln (arrA );
*/
Function desc (x, y)
...{
If (x> y)
Return-1;
If (x <y)
Return 1;
}
Function asc (x, y)
...{
If (x> y)
Return 1;
If (x <y)
Return-1;
}
ArrA. sort (desc); // sort by desc
Document. writeln (arrA );
Document. writeln ("<br> ");
ArrA. sort (asc); // sort by asc
Document. writeln (arrA );


// Output result:
6, 5, 4, 3, 2, 1
1, 2, 3, 4, 5, 6

In addition, an unnamed function can be directly put into the call of the sort () method. In the following example, the odd number is placed at the top and the even number is placed at the bottom. The example is as follows:

The Code is as follows: Copy code


Var arrA = [6, 2, 4, 3, 5, 1];
ArrA. sort (function (x, y )...{
If (x % 2 = 0)
Return 11;
If (x % 2! = 0)
Return-1;
}
);
Document. writeln (arrA );


// Output: 1, 5, 3, 4, 6, 2

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.