compareTo method for names I'm implementing the compareTo method to set the natural order of an object. My object has three fields that make up a name. fName, lName, middleInitial. The natural ordering should be by lName, fName, middleInitial. I was wondering if this is an efficient way of implementing the compareTo.

    @Override
public int compareTo(Employee o) {
    StringBuilder builder = new StringBuilder();
    String thisName;

    if(!StringUtils.isEmpty(this.lName.trim())){
        builder.append(this.lName);
    }
    if(!StringUtils.isEmpty(this.fName.trim())){
        builder.append(this.fName);
    }
    if(!StringUtils.isEmpty(this.midInitial.trim())){
        builder.append(this.midInitial);
    }
    thisName = new String(builder.toString());
    builder = new StringBuilder();

    if(!StringUtils.isEmpty(o.lName.trim())){
        builder.append(o.lName);
    }
    if(!StringUtils.isEmpty(o.fName.trim())){
        builder.append(o.fName);
    }
    if(!StringUtils.isEmpty(o.midInitial.trim())){
        builder.append(o.midInitial);
    }

    if(!StringUtils.isEmpty(thisName) &amp;&amp; !StringUtils.isEmpty(builder.toString())){
        return thisName.compareTo(builder.toString());
    }else{
        return 0;
    }

}
Correctness


How should these names be ordered?


SMITH, ARIEL B.
SMITH, ZINNIA X.
SMITHSON, HARRIET


The way your code works, you smash all the components of each name together, like "SMITHZINNIAX" and "SMITHSONHARRIET", resulting in Harriet Smithson coming before Zinnia Smith.  Thus, you'll end up Ms. Smithson ordered between the two Smiths, violating the expectation that all employees with the last name Smith be listed together.

Therefore, you should be comparing by lName first, proceeding to compare by fName only to break ties.
If one of the employees has no name (lName, fName, and midInitial are all empty strings after trimming), then this compareTo() method will report it as being equal to all other employees!


Performance

That's a huge amount of string manipulation to do a comparison!  That's a bad idea, because if you want to sort a list of employees, you will likely end up doing many of these comparisons, and each comparison will be much slower than it should be.

You should be able to implement this compareTo() method without any string concatenation or trimming.

Design


Why do you need to .trim() any of these name components?  Is your data unclean?  Why are initial and trailing spaces being stored in any of these fields?  Such spaces would not be considered a legitimate meaningful part of a name, so why aren't they either rejected or sanitized in the constructor or setter methods?
Unlike numbers or strings, which have an inherent natural ordering, there is no obvious reason why employees should be ordered according to their names.  What if you want to sort a list of employees by their salary instead?  Instead of writing this Employee.compareTo() method, consider moving this code out into a Comparator class instead:

import java.util.Comparator;

public class Employee {
    public String getLastName() { … }
    public String getFirstName() { … }
    public String getMidInitial() { … }

    public static final Comparator&lt;Employee&gt; COMPARE_BY_NAME = new Comparator&lt;Employee&gt;() {
        @Override
        public int compare(Employee a, Employee b) {
            int cmp;
            if (0 != (cmp = a.getLastName().compareTo(b.getLastName()))) {
                return cmp;
            }
            if (0 != (cmp = a.getFirstName().compareTo(b.getFirstName()))) {
                return cmp;
            }
            return a.getMidInitial().compareTo(b.getMidInitial());
        }
    };
}


Since Java 8, you can also write:

public class Employee {
     …

    public static final Comparator&lt;Employee&gt; COMPARE_BY_NAME =
        Comparator.comparing(Employee::getLastName)
                  .thenComparing(Employee::getFirstName)
                  .thenComparing(Employee::getMidInitial);
    }
}