Sort Javascript arrays by sort () and reverse ()

Source: Internet
Author: User

Where:

1. If no comparison function is specified in JavaScript sort (), the comparison function is sorted in ascending order by character encoding by default. That is to say, if we want to sort the values, we do not necessarily want the results.

2. Javascript reverse () sorts the elements in the array in reverse order.

First look at the first point above. If there is an array arr = [, 9], use arr. after sort (), the order of the array is 1, 3, 6, 7, 9, and the expected result is obtained.

Let's take a look at the following array sorting: arr = [3, 1, 16, 34, 30]. If we run arr. sort (), will we still get what we want, 3, 16, 30, 34?

After the execution, we found that the results were:, and 34. Obviously, the results were not what we wanted. In fact, the sort method sorts the preceding values by string, that is, it is equivalent to array arr1 = ['3', '1', '16', '34 ', the sorting results of '30'] are consistent.

The Code is as follows:Copy codeThe Code is as follows: var arr = [3, 1, 16, 34, 30];
Var arr1 = ['3', '1', '16', '34', '30'];
Alert (arr. sort (); // 1, 16, 3, 30, 34
Alert (arr1.sort (); // 1, 16, 3, 30, 34

So if we want to get the correct results: 1, 3, 16, 30, 34, how should we do it?

Query the javascript manual, which is described as follows:

Definition and usage

The sort () method is used to sort the elements of an array.

Syntax

ArrayObject. sort (sortby) parameter description
Sortby is optional. Specify the sorting order. Must be a function.

Return Value

Array reference. Note that arrays are sorted on the original array without generating copies.

Description

If no parameter is used when this method is called, the elements in the array are sorted alphabetically. More precise, the elements are sorted by character encoding. To achieve this, first convert the elements of the array into strings (if necessary) for comparison.

If you want to sort by other criteria, you need to provide a comparison function that compares two values and returns a number indicating the relative sequence of the two values. The comparison function should have two parameters, a and B. The returned values are as follows:

If a is less than B, a should appear before B in the sorted array, then a value smaller than 0 is returned.
If a is equal to B, 0 is returned.
If a is greater than B, a value greater than 0 is returned.
============================================

From the preceding descriptions, we can understand that if you want to sort values, you need to provide a comparison function. Common comparison functions are as follows:Copy codeThe Code is as follows: function sortArr (m, n ){
If (m <n)
Return-1; // less than, return-1
Else if (m> n)
Return 1; // greater than, return 1
Else return 0; // equals, return 0
}

The simplified form can be written as follows:Copy codeThe Code is as follows: function sortArr (m, n ){
Return m-n;
}

Copy codeThe Code is as follows: function sortArr (m, n ){
Return m> n? 1 :( m <n? -1:0 );
}

Then execute arr. sort (sortArr) and find that we can get the expected results:, 34. That is to say, the array is sorted in ascending order by integer values.
In this case, the new problem arises. How can we sort the array in descending order?

One idea is to change the return value of the sortArr function. If m is less than n, integrity is returned. If m is greater than n, negative values are returned. If m is n, 0 is returned. In this way, you can.

You can write two functions in ascending or descending order. Then you can call different functions according to different needs.

In addition, we can also call another function reverse () mentioned above to implement it easily. When we sort the data in ascending order, then the array calls the reverse () method to reverse the array, in this way, the array can be sorted in descending order.

The Code is as follows:Copy codeThe Code is as follows: arr. sort (sortArr). reverse ();

Summary: This section mainly introduces the sorting of arrays in Javascript. Because arrays are sorted by strings by default, You need to define a comparison function to sort them by other rules.

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.