title: Sort subarrays according to their first element
body: <p>I have an array of objects, let's say each object is of a type Grid.</p>
  
  <p>Each Grid object has x and y coordinates, </p>
  
  <pre><code>Grid temp = new Grid(3, 5);
  // temp.x returns x, temp.y returns y.
  </code></pre>
  
  <p>Now I have a few arrays of Grid</p>
  
  <pre><code>Grid[] theArray1 = new Grid[5];
  Grid[] theArray2 = new Grid[5];
  Grid[] theArray3 = new Grid[5];
  </code></pre>
  
  <p>I fill the arrays up with Grid objects and sort them using Arrays.sort.</p>
  
  <p>I now join the sorted arrays to form theArray with length 5+5+5=15.</p>
  
  <p>I now want to sort theArray by the first element of the "subarrays" (elements 0, 5 and 10 in theArray)</p>
  
  <p>How could I achive this? Also, if there is a simpler way to achieve the same result, that would be nice. But I have to start with the 3 arrays since they are gotten through an iteration of a for-loop.</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>Example: Let's say I sort by X coordinated, smaller first.
  I'll make each Grid[] of length 3 instead of 5 for simplicity.</p>
  
  <pre><code>Grid[] theArray1 = new Grid[]{new Grid(2, 1), new Grid(4, 1), new Grid(0, 1)};
  Grid[] theArray2 = new Grid[]{new Grid(4, 2), new Grid(3, 1), new Grid(7, 1)};
  Grid[] theArray3 = new Grid[]{new Grid(1, 7), new Grid(5, 3), new Grid(10, 1)};
  </code></pre>
  
  <p>What I want to end up with is an array/arraylist that, when printed, prints like this:</p>
  
  <pre><code>for (int i = 0; i &lt;= theArray.length-2; i++) {
      StdOut.println(theArray[i] + ", " + theArray[i+1] + ", " + theArray[i+2] + "\n");
  }
  
  // Output:
  
  (0, 1), (2, 1), (4, 1) //this is theArray1
  (1, 7), (5, 3), (10, 1) //this is theArray3
  (3, 1), (4, 2), (7, 1) //this is theArray2
  </code></pre>
  
  <p>First I sort each theArray(1, 2 and 3) so that the element with the lowest x coordinate is first, then second smallest, then largest.</p>
  
  <p>Then I arrange these arrays by the size of the first element of each of them. theArray3 goes before theArray2 because the x coordinate of the first element is 1 but 3 in theArray2</p>