Array - sort()
<html>
<body>
<script type="text/javascript">
array1=new Array("Red","Blue","Green")
array2=new Array("73","19","830")
array3=new Array(510,130,23,3400)
array4=new Array("72","83","850",3,1,5,40)
function compareNum(a,b){
return a-b
}
document.write("Sorted: " + array1.sort())
document.write("<br><br>")
document.write("Sorted without compareNum: " + array2.sort())
document.write("<br>")
document.write("Sorted with compareNum: " + array2.sort(compareNum))
document.write("<br><br>")
document.write("Sorted without compareNum: " + array3.sort())
document.write("<br>")
document.write("Sorted with compareNum: " + array3.sort(compareNum))
document.write("<br><br>")
document.write("Sorted without compareNum: " + array4.sort())
document.write("<br>")
document.write("Sorted with compareNum: " + array4.sort(compareNum))
</script>
</body>
</html>
Related examples in the same category