Person p1 = (Person)o1; Person p2 = (Person)o2; int ncomp = p1.getLastName().compareTo(p2.getLastName()); // If the last names are equal, check the first names if (ncomp == 0) { ncomp = p1.getFirstName().compareTo(p2.getFirstName()); } return ncomp;