I have 7 letters on my screen, 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)
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 Y = 27
sort them by X
where the smallest is put in front of the ArrayList
etc below is my code:
private static void sort(ArrayList<BoxLetter> letters){
for(int i = 0; i < letters.size(); i++){
for(int j = letters.size() -1; j > i; j--){
if(letters.get(i).getCentroid().y > letters.get(j).getCentroid().y){
if(letters.get(i).getCentroid().x > letters.get(j).getCentroid().x){
BoxLetter temp = letters.get(i);
letters.set(i, letters.get(j));
letters.set(j, temp);
Logger.printMessage("Sorting");
}
}
}
}
}
When I run my code the output is ADBCEFG
but I expect it to output ABCDEFG
My question is, How can I sort the list in the form ABCDEFG
in Java 7?