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

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

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

<script> 
 

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 
document.write("<br/>"); 

//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("<br/>"); 

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

</script> 

</head> 

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

</html> 

Related articles: