Skip to content

Instantly share code, notes, and snippets.

@DavidMah
Created December 13, 2011 06:56
Show Gist options
  • Save DavidMah/1470979 to your computer and use it in GitHub Desktop.
Save DavidMah/1470979 to your computer and use it in GitHub Desktop.
compareTo with two fields
public int compareTo(Human other) {
if(age < other.age) {
return -1;
} else if (age > other.age) {
return 1;
} else {
// Fall back to the logic for the second field only if the first two are equal.
if(guiltyPleasure < other.guiltyPleasure) {
return -1;
} else if(guiltyPleasure > other.guiltyPleasure) {
return 1;
} else {
return 0;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment