Web Analytics Made Easy -
StatCounter Sort 2D Arrray - CodingForum

Announcement

Collapse
No announcement yet.

Sort 2D Arrray

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new 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
    Daily Hot Deal

  • #2
    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.

    Comment


    • #3
      this is perfect thank you Philip M
      Daily Hot Deal

      Comment

      Working...
      X