Introduction to the use of the sort of method for arrays in javascript

  • 2020-03-30 00:57:10
  • OfStack

<title> An array of sort() methods </title> 


var arr = [2,4,8,1,22,3]; 
var arrSort= arr.sort();//Not properly sorted, the array is converted to a string and then sorted
document.write(" The default sort array is: " + arrSort);//1,2,22,3,4,8 

//The comparison function
function mysort(a,b){ 
return a-b; 

var arrSort2 = arr.sort(mysort);// The incoming The comparison function
document.write(" The array of comparison parameters passed in is: " + arrSort2);//The correct order

document.write(" The original array is: " + arr); 



<div id="time"></div> 


Related articles: