Using bubble sort on strings in ArrayList with multiple inputs : <p>I have trouble using bubble-sort on my ArrayList sorting them in an alphabetical order. </p>

<p>I tried to read up (and copy) some of the bubble sorting methods i've found online but nothing has worked so far. </p>

<pre><code>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 &lt; sortedUsers.size(); x ++) {
                    if (sortedUsers.get(x).getName().compareTo(sortedUsers.get(x+1).getName()) &gt; 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);

        }
    }
}
</code></pre>

<p>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. </p>
