Among them:

1. JavaScript sort () if you do not specify a comparison function, the default will be according to the coding sequence in ascending order of the characters. That is to say, if we want to prioritize the numerical result is not necessarily what we want.

2. Javascript reverse () will reverse the elements of an array.

first take a look at the above the first point, if there is an array arr=,6,3,7,9 [1], the use of arr. Sort (), after the order of the array is 1,3,6,7,9, get the results we want.

and look at the following order: an array arr=,1,16,34,30 [3], if you execute arr. Sort () will be 1,3,16,30,34 we want?

implementation after we found the results as follows: 1,16,3,30,34, obviously the result is not what we want. In fact, the sort method will be ordered the above numerical way according to the string, that is to say, and array arr1=[' 3 ', '1', '16', '34', '30'] sort of consistent results.

code is as follows:

var arr=,1,16,34,30 [3];
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 right result: 1,3,16,30,34 should how to do?

query javascript handbook, the instructions in the manual are as follows:

to define and use the

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

arrayObject. Sort (sortby) parameters description
sortby is optional. Sort order. Must be a function. Return values

an array of references. Please note that on the original array to sort an array, not a copy of the generated.


if you call this method when the parameter is not used, will be sorted in alphabetical order of the elements in the array, speak more accurately, according to the character encoding is the order of the sort. To do this, you should take the array elements are converted to string (if necessary), in order to compare.

if you want to be sorted according to other standards, you need to provide a comparison function, the function to compare two values, and then returns a is used to illustrate the two values of the relative order of Numbers. Comparison function should have two parameters a and b, the return value is as follows:

if a less than b, a, in a sorted array should appear before b, it returns a value less than 0.
if a is equal to b, it returns 0.
if a greater than b, then returns a value greater than 0.

by the above we can see that if we are to be carried out in accordance with the numerical sequence, then provide a comparison function. General common comparison function is as follows:

function sortArr (m, n) {
if (m return - 1;//less than, return 1
else if (m> n)
return 1;//is greater than, return 1
the else return 0;//equal to the return 0

leaner can be written as the following two forms:

function sortArr (m, n) {
return m - n.

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

then execute the arr. Sort (sortArr), found that can get the results we want: 1,3,16,30,34. Said is carried out in accordance with the integer array in ascending order.
so new problem comes, if we want to sort an array in descending order do?

there is a way of thinking is change sortArr function return values, if m N return a negative value, it returns 0 if m=n. This is ok.

you can write two functions, an ascending and a descending order. Then according to the different functions of different need to call it.

in addition we can also call the above mentioned another function reverse () easy to realize, when we are in ascending order, and then the array in the call reverse () method will be an array of reverse, so that it can realize the descending order of the array.

code is as follows:

arr. Sort (sortArr). Reverse ();

summary: mainly introduces the Javascript array sort here, since the default in accordance with the string sorting, to achieve, in accordance with the rules of other forms of sorting their defined comparison function.


  • javascript数组去重3种方法的性能测试与比较
  • 两种常用的javascript数组去重方法思路及代码
  • javascript数组去掉重复
  • 有趣的JavaScript数组长度问题代码说明
  • 有趣的javascript数组定义方法
  • COM中获取JavaScript数组大小的代码
  • JavaScript数组应用 可依次读取的公告栏文字
  • 用Javascript数组处理多个字符串的连接问题
  • JavaScript数组问题解决的多种方法
  • javascript数组的扩展实现代码集合
  • javascript数组使用调用方法汇总
  • javascript数组组合成字符串的脚本
  • 给Javascript数组插入一条记录的代码
  • JavaScript数组的快速克隆(slice()函数)和数组的排序、乱序和搜索(sort()函数)
  • javascript数组的使用

This concludes the body part