Q:

Sort Users using Insertion Sort

I am trying to sort users in ascending order when I search for the users in our UI. I started the approach with Insertion Sort and I currently have something like this: 
if (CollectionUtils.isNotEmpty(userList)) {
            //Sort by ascending by display name
            for (int i=1; i<userList.size(); i++){
                User key = userList.get(i);
                int j = i-1;
                System.out.println("I: "+key.getFirstName());
                System.out.println("J: "+userList.get(j).getFirstName());
                while (j>-1 && isAscending(key.getFirstName(), userList.get(j).getFirstName())){
                    User temp = userList.get(j+1);
                    System.out.println("TEMP: "+temp.getFirstName());
                    userList.add(j+1, userList.get(j));
                    userList.add(j, temp);
                    j--;
                }
                userList.add(j+1, key);
            }
            for (final User user : userList) {
                beanList.add(UserBean.getInstance(user));
            }
        }

Assuming that we have nulls, my isAscending method checks for that and the idea is that the users who's name is null will be placed at the bottom of the list: 
private boolean isAscending(String left, String right){
        if(left.equals(right)) return false;
        if(left == null && right == null) return false;
        if((left == null && right != null) || left != null && right == null){
            return false;
        }
        if(left.toLowerCase().compareTo(right.toLowerCase())>0){
            return true;
        }

        return false;
    }

With these 2 methods I want to be able to sort users in an ascending order based on their first name. Currently I am running into an infinite loop as after the first iteration, the user at the first index is being compared to himself (aka the user at the first index) over and over again. 
Any suggestion would be welcome. Thanks

A:

One of your problems is that you keep adding stuff to the list, but you never remove anything. For example, your inner loop is:
            while (j>-1 && isAscending(key.getFirstName(), userList.get(j).getFirstName())){
                User temp = userList.get(j+1);
                System.out.println("TEMP: "+temp.getFirstName());
                userList.add(j+1, userList.get(j));
                userList.add(j, temp);
                j--;
            }

Let's say the list contains [3,2,1]. So the first time through you get:
temp = userList.get(1)  // so temp = 2
userList.add(1, 3);  // this makes the list contain [3,3,2,1]
userList.add(j, temp) // result: [2,3,3,2,1]

You want to swap the values at locations j and j+1. So write:
temp = userList.get(j+1);
userList.set(j+1, userList.get(j));
userList.set(j, temp);

Also, you probably should fix your isAscending method. Currently, you're calling equals on a potentially null reference. Also, there's no reason to call toLowerCase on the parameters to compareTo when you can easily call compareToIgnoreCase. A cleaner comparison method is:
private boolean isAscending(String left, String right){
    // if either parameter is null, then not ascending
    if(left == null || right == null) return false;

    return (left.compareToIgnoreCase(right) > 0);
}

You might reconsider the null handling logic in your code. Does it really make sense for there to be null values for the fields you're comparing? If not, then you probably should have the comparison method throw an exception.
If you do want to support null values, then you need to modify your isAscending method because the current logic makes any non-null value equal to any null value. So if your list contained [3,null,1], then sorting it would return the same order because calling isAscending(3, null) will return false, and so will isAscending(null, 1). If you want to sort nulls to the front, you want this:
    // both values are null, so not ascending
    if (left == null && right == null) return false;

    // left is null, but right is not
    if (left == null) return true;

    // right is null, but left is not
    if (right == null) return false;

    return (left.compareToIgnoreCase(right) > 0);

