Home > Web Front-end > JS Tutorial > body text

Sorting of Javascript arrays sort() method and reverse() method_javascript skills

WBOY
Release: 2016-05-16 17:52:48
Original
1516 people have browsed it

Among them:

1. If no comparison function is specified in JavaScript's sort(), it will be sorted in ascending order by character encoding order by default. That is to say, if we want to sort the values, we may not get the results we want.

2.Javascript's reverse() reverses the order of the elements in the array.

Look at the first point above. If there is an array arr=[1,6,3,7,9], after using arr.sort(), the order of the array is 1,3,6 ,7,9, we got the results we wanted.

Look at the following array sorting: arr=[3,1,16,34,30], if we execute arr.sort(), will we still get the 1,3 we want? What about 16, 30, 34?

After execution, we found that the results are: 1,16,3,30,34. Obviously the results are not what we want. In fact, the sort method sorts the above values ​​in string format, which is consistent with the sorting result of the array arr1=['3','1','16','34','30'].

The code is as follows:

Copy code The 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


Then if We want to get the correct results: 1,3,16,30,34. How should we do it?

Check the javascript manual. The instructions in the manual are as follows:

Definition and usage

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

Syntax

arrayObject.sort(sortby) Parameter Description
sortby Optional. Specifies the sort order. Must be a function.

Return value

A reference to the array. Please note that the array is sorted on the original array and no copy is made.

Explanation

If no parameters are used when calling this method, the elements in the array will be sorted alphabetically, or to be more precise, in the order of character encoding. To achieve this, first convert the array elements into strings (if necessary) for comparison.

If you want to sort by other criteria, you need to provide a comparison function, which compares two values ​​and returns a number describing the relative order of the two values. The comparison function should have two parameters a and b, and its return value is as follows:

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

From the above explanation we It can be understood that if you want to sort by numerical values, then you must provide a comparison function. Common comparison functions are as follows:
Copy code The code is as follows:

function sortArr(m, n){
if(mreturn -1;//less than, return -1
else if(m>n)
return 1;//greater than, return 1
else return 0;//Equal, return 0
}

After being simplified, it can be written in the following two forms:
Copy code The code is as follows:

function sortArr(m,n){
return m-n;
}

Copy code The code is as follows:

function sortArr(m,n){
return m>n?1 :(m}

Then execute arr.sort(sortArr) and find that we can get the results we want: 1,3,16,30 ,34. In other words, the array is sorted in ascending order according to integer values.
In this case, a new question arises. What if we want to sort the array in descending order?

One idea is to change the return value of the sortArr function. If mn, it returns a negative value, and if m=n, it returns 0. That's it.

You can write two functions, one for ascending order and one for descending order. Then just call different functions according to different needs.

In addition, we can also call another function reverse() mentioned above to achieve it easily. When we sort the array in ascending order, then the array calls the reverse() method to reverse the order of the array, so that the array can be implemented Sorted in descending order.

The code is as follows:
Copy code The code is as follows:

arr.sort (sortArr).reverse();

Summary: This article mainly introduces the sorting of arrays in Javascript. Since the default is to sort by strings, to achieve sorting according to other forms of rules, you must define your own comparison function. .
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!