Sort of JavaScript arrays: Sort () method and reverse () method

Source: Internet
Author: User
Tags array array sort arrays execution functions numeric value string sort

Article Introduction: the ordering of arrays in JavaScript.

JavaScript provides the sort () method and the reverse () method so that we can simply sort and reverse the array. which

If you do not specify a comparison function in the sort () of 1.JavaScript, the default is sorted in ascending order by character encoding. That is, if we want to sort the numbers, we don't necessarily have the results we want.

The reverse () of the 2.Javascript reverse the elements in the array.

First look at the 1th above, if there is an array of arr=[1,6,3,7,9], after using Arr.sort (), the order of the array is 1,3,6,7,9, get the result we want.

Take a look at the following array sort: arr=[3,1,16,34,30], if the execution of Arr.sort () is still going to get the 1,3,16,30,34 we want?

After execution we found the result to be: 1,16,3,30,34, obviously the result is not what we want. In fact, the sort method sorts the values above by string, meaning that the order of the array arr1=[' 3 ', ' 1 ', ' 16 ', ' 34 ', ' 30 ' is consistent.

The code is as follows:

			var arr=[3,1,16,34,30];
    var arr1=[' 3 ', ' 1 ', ' ', ', ', ';
    Alert (Arr.sort ());         1,16,3,30,34
    alert (Arr1.sort ());        1,16,3,30,34

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

Query the JavaScript manual, as described in the manual:


Definitions and usage

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


Arrayobject.sort (SortBy)
Parameters Description
SortBy Optional. Specify the sort order. Must be a function.

return value

The reference to the array. Note that the array is sorted on the original array and no replicas are generated.


If the method is invoked without parameters, the elements in the array are sorted alphabetically, and more precisely, sorted in the order of character encoding. To do this, you should 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 then returns a number that describes the relative order of the two values. The comparison function should have two parameters A and B, and the return value is as follows:

    • If a is less than B, a value less than 0 is returned in the sorted array before a should appear in B.
    • If a equals B, it returns 0.
    • If a is greater than B, a value greater than 0 is returned.


We can see from the above that if you want to sort by numeric value, you should provide a comparison function. The most common comparison functions are as follows:

function Sortarr (m,n) {
        if (m<n)
            return-1;//is less than, returns-1
			if (m>n) return
            1;//greater than, returns 1
			return 0;//equals, returns 0
It can be written in the following two ways:
function Sortarr (m,n) {return

function Sortarr (m,n) {return
    m>n?1: (m<n?-1:0);

Then execute Arr.sort (Sortarr) and find out what we want to do: 1,3,16,30,34. The array is sorted in ascending order by integer value.
So the new question comes up, what do we do if we want to sort the array in descending order?

One idea is to change the return value of the Sortarr function, if return integrity when m<n, m>n return negative value, M=n return 0. That's it.

You can write two functions, one ascending, one descending. Then call a different function according to the different needs.

In addition we can call the other function mentioned above reverse () for easy implementation, when we sort in ascending order, then the array in the call to the reverse () method to reverse the array, so that we can implement the descending sort of the array.

The code is as follows:

Arr.sort (Sortarr). reverse ();

Summary: Here is a description of the ordering of the arrays in JavaScript, because the default is sorted by string, to implement the sort of other forms of rules to define their own comparison functions.

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: 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.