...

View Full Version : Sort 2D Arrray



hiyatran
08-26-2011, 05:48 AM
I check the web and they only show you how to sort the whole array. I would like to be able to sort the subset of the 2D array. Here's my array.


var myArray=new Array(
new Array("af","ad","az","ab"),
new Array("bc","bd","bg","bb","bx"),
new Array("cf","ck","ca","cv","co"),
new Array("dd"));

How would I sort the sub array independently. So only the a's together, then only the b's together, etc.

thanks

Philip M
08-26-2011, 08:26 AM
Like so:-


<script type = "text/javascript">
var myArray=new Array(
new Array("af","ad","az","ab"),
new Array("bc","bd","bg","bb","bx"),
new Array("cf","ck","ca","cv","co"),
new Array("dd"));

for (var i = 0; i<myArray.length; i++) {
myArray[i] = myArray[i].sort();
}

alert (myArray);

</script>


And it comes with a caveat which could be read as a warning. - Commentator BBC Radio 4

hiyatran
08-26-2011, 09:25 AM
this is perfect thank you Philip M



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum