Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3

Thread: Sort 2D Arrray

  1. #1
    New Coder
    Join Date
    Jun 2010
    Posts
    37
    Thanks
    1
    Thanked 0 Times in 0 Posts

    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

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,918
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    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.

  • #3
    New Coder
    Join Date
    Jun 2010
    Posts
    37
    Thanks
    1
    Thanked 0 Times in 0 Posts
    this is perfect thank you Philip M


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •