The summary of JS array sorting method

Source: Internet
Author: User

Bubble sort

The code is as follows Copy Code

<script type= "Text/javascript" >
<!--
function Sort2 (obj)
{
var arr=obj;
var temp;
for (Var i=0;i<arr.length;i++)
{
for (Var j=i+1;j<arr.length;j++)
{
if (Arr[i]<arr[j])
{
Temp=arr[i];
ARR[I]=ARR[J];
Arr[j]=temp;
}
}
}
return arr;
}
Alert (Sort2 ([3,2,5,6,10,8,4,1]));
-->
</script>

Column 2 uses the Sort method for sorting.

The code is as follows Copy Code

<script type= "Text/javascript" >
<!--
function Sort1 (obj)
{
var arr=obj;
Arr.sort (function (a,b) {return a<b?1:-1});
return arr;
}
Alert (Sort1 ([3,2,5,6,10,8,4,1]));
-->
</script>

Sort also has an example

The code is as follows Copy Code

var Arrdemo = new Array ();

Arrdemo[0] = 10;
ARRDEMO[1] = 50;
Arrdemo[2] = 51;
ARRDEMO[3] = 100;

Arrdemo.sort (); When the sort method is called, the array itself is changed, which affects the original array

alert (Arrdemo);//10,100,50,51 By default the sort method is sorted in ASCII alphabetical order, not what we think is sorted by number size

Arrdemo.sort (function (a,b) {return a>b?1:-1});//small to large sort

alert (Arrdemo);//10,50,51,100

Arrdemo.sort (function (a,b) {return a<b?1:-1});//Sort from large to small

alert (Arrdemo);//100,51,50,10

1. When the array calls the sort method, it affects itself (rather than generating a new array)

The 2.sort () method is sorted by character by default, so when you sort a numeric array, you can't take it for granted that it will be sorted by number size!

3. To change the default sort behavior (that is, sort by character), you can specify the collation function yourself (as shown in this example)

Efficiency after I test whether in IE7 or under FF3, are sort1 fast.

If you want to sort through the second column of data in a two-dimensional array:

  code is as follows copy code

<script language= "javascript"
<!--
var testarray = new Array ( );
testarray[0]= New Array (' E ', ' 2 ', ' G ');
testarray[1]= New Array (' B ', ' 3 ', ' C ');
testarray[2]= New Array (' A ', ' 1 ', ' a ');  
testarray[3]= new Array (' d ', ' 4 ', ' h ');  
Testarray.sort (sortfunction);
Alert (testarray[0] + "" + testarray[1] + "+ testarray[2] +" "+ testarray[3])

There are two parameters in the

//Sort method that represent the two elements used to sort in the array
//
Function sortfunction (x,y)
{
 return x[2].charcodeat (0)-y[2]. charCodeAt (0);//Descending sort
}
   //By the ASCII code of the first letter of the third column of the two-dimensional array;
</script>

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.