title: How do i sort a simple integer 2 dimensional array?
body: <p>I have a 2 dimensional array, that has dimensions of <code>myArray[x][3]</code>. I need to sort the array based upon <code>[x][0]</code>. I was using <code>Arrays.sort(myArray);</code>. That was working, however, the array at the time was a one dimension array of <code>myArray[x]</code>. Then I changed my mind and changed it into a 2 dimensional array. It is filled with integers from 1 to 9. I have searched for the clear method to sort 2 dimensional arrays, and cannot find the simple explanations. Please help.</p>
  
  <p>Thanks;
  Ice</p>
  
  <p>Ok, here is the code:</p>
  
  <pre><code>public static void sortArray(int myArray[][]){
  Arrays.sort(myArray, new Comparator&lt;Integer[]&gt;(){
      @Override
      public int compare(Integer[] o1, Integer[] o2) {
          return o1[0].compareTo(o2[0]);
      }
  });
  </code></pre>
  
  <p>Did that work?</p>
  
  <p>OK, here is the problem. The sorted array starts out unsorted, like this:</p>
  
  <pre><code>3 - 0 - 0
  4 - 0 - 1
  5 - 0 - 2
  6 - 0 - 3
  3 - 0 - 4
  </code></pre>
  
  <p>The first column <code>[0][x]</code> is the value, the second column <code>[1][x]</code> is the array field count, and the last column <code>[2][x]</code> is the actual column number in the array. The overall method, takes a entire row from the original 2 dimensional array, and loads it into a 3-tall by x-wide array, then sorts the array based on the <code>[0][x]</code> column. Here is the result after the sort function now being called:</p>
  
  <pre><code>0 - 0 - 3
  0 - 1 - 4
  0 - 2 - 5
  0 - 3 - 6
  0 - 4 - 3
  </code></pre>
  
  <p>Somehow, the method I copied and pasted, is swapping out the numbers, seems like the sorting is wrong. Same <code>System.out.print</code> is being used on both outputs.</p>