I have trouble using bubble-sort on my ArrayList sorting them in an alphabetical order.
I tried to read up (and copy) some of the bubble sorting methods i've found online but nothing has worked so far.
private void listUsers() {
int x;
User temp;
User temp2;
if (users.isEmpty()) {
System.out.println("Error: no users in register");
} else {
for (User item : users) {
sortedUsers.add(item);
if (!sortedUsers.isEmpty()) {
for (x = 0; x < sortedUsers.size(); x ++) {
if (sortedUsers.get(x).getName().compareTo(sortedUsers.get(x+1).getName()) > 0) {
temp = sortedUsers.get(x);
temp2 = sortedUsers.get(x+1);
sortedUsers.set(x, temp2);
sortedUsers.set(x+1, temp);
}
}
}
//System.out.println(item);
System.out.println(item);
}
}
}
As of now i'm getting a java.lang.IndexOutOfBoundsException, I understand that with the x+1 i'm trying to get indexes without items but don't really understand how to fix it.