Comparison method violates its general contract! Java 7 only

see this: From http://www.oracle.com/technetwork/java/javase/compatibility-417013.html#source Area: API: Utilities Synopsis: Updated sort behavior for Arrays and Collections may throw an IllegalArgumentException Description: The sorting algorithm used by java.util.Arrays.sort and (indirectly) by java.util.Collections.sort has been replaced. The new sort implementation may throw an IllegalArgumentException if it detects a Comparable that violates the Comparable contract. The previous implementation silently … Read more

How do I write a compareTo method which compares objects?

This is the right way to compare strings: int studentCompare = this.lastName.compareTo(s.getLastName()); This won’t even compile: if (this.getLastName() < s.getLastName()) Use if (this.getLastName().compareTo(s.getLastName()) < 0) instead. So to compare fist/last name order you need: int d = getFirstName().compareTo(s.getFirstName()); if (d == 0) d = getLastName().compareTo(s.getLastName()); return d;