Introduction to javascript Array sorting functions sort and reverse _ javascript skills

Source: Internet
Author: User
The reverse method reverses the position of elements in an Array object. The sort method returns an Array object whose elements have been sorted. Next we will introduce the reverse method first.

The reverse method reverses the element positions in an Array object. During execution, this method does not create a new Array object.

For example:

The Code is as follows:


Var array1 = ['A', 'cc', 'bb', 'Hello', false, 0, 3];
Var array2 = [3, 5, 2, 1, 7, 9, 10, 13];
Array1.reverse ();
Array2.reverse ();
Alert (array1 );
Alert (array2 );


If the array contains only numbers, the numbers are sorted in descending order. If the array contains other types, the array is reversed and the array is returned.

Sort Method

Returns an Array object whose elements have been sorted.

The Code is as follows:


Arrayobj. sort (sortfunction)


Parameters

ArrayObj

Required. Any Array object.

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.

Example 1 :()

The Code is as follows:


Var a, l; // declare the variable.
A = ["X", "y", "d", "Z", "v", "m", "r", false, 0];
L = a. sort (); // sort the array.
Alert (l); // returns the sorted array.


In this example, if the comparison function is not input, the elements are sorted in ascending order according to the ASCII character order. In addition, this array contains multiple types of data, even if the comparison function is input, it is still in ascending order of ASCII characters.

For example

The Code is as follows:


Var a, l; // declare the variable.
A = ["X", "y", "d", "Z", "v", "m", "r", false, 0];
L = a. sort (); // sort the array.
Alert (l); // returns the sorted array.
Ll = a. sort (compack );
Alert (ll); // returns the same result as above
Function compack (a, B ){
Return a-B;
}


When you need to sort numbers, you can use the sort method. You only need to input a comparison function to sort numbers in ascending or descending order.

Ascending Order:

The Code is as follows:


Var a, l; // declare the variable.
A = [6, 8, 9, 5.6, 90];
L = a. sort (compack); // sort the array.
Alert (l); // returns the sorted array.

Function compack (a, B ){
Return a-B;
}


Descending order:

The Code is as follows:


Var a, l; // declare the variable.
A = [6, 8, 9, 5.6, 90];
L = a. sort (compack); // sort the array.
Alert (l); // returns the sorted array.

Function compack (a, B ){
Return B-;
}


In the comparison function, a-B is returned in ascending order, and B-a is returned in descending order.

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.