title: Jquery JavaScript create dynamic 2 dimensional array
body: <p>Hi I see there are loads of examples but none explains what I need done.</p>
  
  <p>I want to create and add items to a 2 Dimensional array and sort dynamically.</p>
  
  <p>Some code I have been messing around with:</p>
  
  <pre><code>var Amount = new Array(new Array());  //MULTI ARRAY
  var a = 0; //COUNTER
  
  $("input[id^='AmountSpent']").each(function(){
      Amount[a][a] = [a, $(this).val()]; //THIS IS WHERE I GET STUCK... HOW TO ASSIGN VALUES
      a = a + 1;
  });
  </code></pre>
  
  <p>After that I want to sort the array.</p>
  
  <p>So if the array looks like this:</p>
  
  <pre><code>Amount = [[1,2,3,4],[$200,$300,$100,$600]]
  </code></pre>
  
  <p>I want to sort highest amount first: <code>$600, $300, $200, $100</code></p>
  
  <p>Can anyone please help me. </p>
  
  <hr>
  
  <p>U P D A T E</p>
  
  <hr>
  
  <p>Using the code i got from Rory(<em>Thanks a lot</em>) I am doing the following:</p>
  
  <pre><code>var amounts = [];    
  $("input[id^='AmountSpent']").each(function(i, el){
    amounts.push({ index: i + 1, value: $(el).val() });
  });
  amounts.sort(function(a, b) {
    if(a.value &lt; b.value) return 1;
    if(a.value &gt; b.value) return -1;
    return 0;
  });
  </code></pre>
  
  <p>To loop through the array I am doing :</p>
  
  <pre><code>for (ii = 0; ii &lt; amounts.length; ++ii) {
  console.log(amounts[ii].index + " - " +  amounts[ii]); // 
   }
  </code></pre>
  
  <p>The Result I get is :</p>
  
  <p>1 - [object object]<br>
  2 - [object object]<br>
  3 - [object object]<br></p>