Insertion sort Java algorithm in an arraylist String
<p>So I have an arraylist that I want that I need to sort with an insertion sort algorithm for a programming class. I have this and ArrayList containing these String's = (Andrew, Felip, Juan, Camil, Jhon, William) and I have this Java code:</p>

<pre><code>public void insertionSort( )
    {

        ArrayList&lt;Reserve&gt; array = giveReserves();

        for(int i = 1   ; i &lt; array.size()-1; i++)
        {
            Reserve element = array.get(i);
            String nomI = element.giveNameClient();
            int j = i;
            String nomJM = array.get(j-1).giveNameClient();
            String nomJ = array.get(j).giveNameClient();
            while(j &gt; 0 &amp;&amp; (nomJM.compareTo(nomJ) &gt; 0))
            {
                Reserve temp = array.get(j);
                array.set(j, array.get(j-1));
                array.set(j-1, temp);
                j = j-1;
            }
        }
    }
</code></pre>

<p>So I have an hotel that has an ArrayList of reserve's, each reserve has the name of the client that did it. What I want to do, is to sort the Reserve ArrayList by client name.</p>

<p>So I have a method that prints each name client, like this:</p>

<pre><code>public void showNames()
    {
        for(Reserve x: reserves)
        {
            System.out.print(x.giveNameClient() +" ");
        }
    }
</code></pre>

<p>in the main() method I print the names of the clients before sorting and then sorted. btw the arraylist of reserves is in a class named Test.</p>

<pre><code>public static void main(String args[])
    {
        Test object = new Test();
        System.out.println("Names: ");
        object.showNames();
        object.insertionSort();
        System.out.println();
        System.out.println("after sorting: ");
        object.showNames();
    }
</code></pre>

<p>Now when I compile this I get the following:</p>

<pre><code>Names:
Juan Jhon Camil William Andrew Felip
after sorting: 
Andrew Camil Jhon Juan William Felip
</code></pre>

<p>The thing is that the output should be Andrew Camil Felip Jhon Juan William </p>

<p>Thanks.</p>