Greelmo
06-24-2003, 09:54 PM
How do i use the .sort(); methode with an array to make it so that the elements inside are sorted from least to greatest?... they are all numbers from 2 through 14.
|
Click to See Complete Forum and Search --> : Sorting an array... Greelmo 06-24-2003, 09:54 PM How do i use the .sort(); methode with an array to make it so that the elements inside are sorted from least to greatest?... they are all numbers from 2 through 14. Exuro 06-24-2003, 11:25 PM Here's a little something I whipped up: <HTML> <HEAD> <SCRIPT LANGUAGE="JavaScript"> <!-- var nums = new Array(); nums = [2,11,4,7,3,14,5,12,10,9,13] function sizeSort(a, b) { if(a < b) return -1 if(a > b) return 1 return 0 } document.write(nums.sort(sizeSort)) // --> </SCRIPT> </HEAD> </HTML> And when you look at that in the browser it gives you this: 2,3,4,5,7,9,10,11,12,13,14 What I did was included a "compare function" for the sort method. What it does is tells the method how to sort the items in the array. If you want to know more you can read about it here: http://www.devguru.com/Technologies/ecmascript/quickref/Sort.html Hope that helped! Gopinath 06-25-2003, 04:06 AM var arr1=new Array("a1","b2","F6","D4","E5","C3") arr1.sort() for(var int_count=0; int_count<arr1.length; int_count++) { alert(arr1[int_count]); } Greelmo 06-25-2003, 07:54 PM THANKS GUYS! webdeveloper.com
Copyright Internet.com Inc., All Rights Reserved. |