title: Sorting arrayList of Coordinates Java
body: <p>I have 7 letters on my screen, <code>A (X = 24, y = 27), B (X = 38, y = 27), C (X = 54, y = 26), D (X = 18, y = 59), E (X = 33, y = 58), F (X = 49, y = 58) and G (X = 63, y = 57)</code> when I put my coordinates in an ArrayList they enter randomly, I want to sort them in the order they are displayed, where I sort them as I go down the Y axis, so for example I get all coordinates on <code>Y = 27</code> sort them by <code>X</code> where the smallest is put in front of the <code>ArrayList</code> etc below is my code:</p>
  
  <pre><code>private static void sort(ArrayList&lt;BoxLetter&gt; letters){
  
  
      for(int i = 0; i &lt; letters.size(); i++){
  
          for(int j = letters.size() -1; j &gt; i; j--){
  
                  if(letters.get(i).getCentroid().y &gt; letters.get(j).getCentroid().y){
  
                      if(letters.get(i).getCentroid().x &gt; letters.get(j).getCentroid().x){
  
                          BoxLetter temp = letters.get(i);
                          letters.set(i, letters.get(j));
                          letters.set(j, temp);
                          Logger.printMessage("Sorting");
                      }
                 }
          }
      }
   }
  </code></pre>
  
  <p>When I run my code the output is <code>ADBCEFG</code> but I expect it to output <code>ABCDEFG</code>
  My question is, How can I sort the list in the form <code>ABCDEFG</code> in Java 7?</p>