Created
December 13, 2011 06:56
-
-
Save DavidMah/1470979 to your computer and use it in GitHub Desktop.
compareTo with two fields
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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