Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-26-2011, 04:48 AM   PM User | #1
hiyatran
New Coder

 
Join Date: Jun 2010
Posts: 36
Thanks: 1
Thanked 0 Times in 0 Posts
hiyatran is an unknown quantity at this point
Sort 2D Arrray

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.

Code:
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
__________________
Daily Hot Deal
hiyatran is offline   Reply With Quote
Old 08-26-2011, 07:26 AM   PM User | #2
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,037
Thanks: 197
Thanked 2,411 Times in 2,389 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Like so:-

Code:
<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
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
Philip M is online now   Reply With Quote
Old 08-26-2011, 08:25 AM   PM User | #3
hiyatran
New Coder

 
Join Date: Jun 2010
Posts: 36
Thanks: 1
Thanked 0 Times in 0 Posts
hiyatran is an unknown quantity at this point
this is perfect thank you Philip M
__________________
Daily Hot Deal
hiyatran is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:52 AM.


Advertisement
Log in to turn off these ads.