Array - sort() : Array : Language Basics JAVASCRIPT DHTML TUTORIALS


JAVASCRIPT DHTML TUTORIALS » Language Basics » Array »

 

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>

           
       



-

Leave a Comment / Note


 
Verification is used to prevent unwanted posts (spam). .

Follow Navioo On Twitter

JAVASCRIPT DHTML TUTORIALS

 Navioo Language Basics
» Array