Created
September 23, 2014 23:32
-
-
Save arturotena/14d275f25fc8e710d70a to your computer and use it in GitHub Desktop.
One option to implement Comparable.compareTo()
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
// I haven't tested it | |
public int compareTo(MyClass that) { | |
if (this == that) return 0; | |
int comp = Integer.valueOf(this.intField).compareTo(that.intField); | |
if (comp != 0) return comp; | |
comp = this.stringField == null ? -1 : this.claveRed.compareTo(that.stringField); | |
if (comp != 0) return comp; | |
comp = Boolean.valueOf(this.booleanField).compareTo(that.booleanField); | |
if (comp != 0) return comp; | |
comp = this.objectField == null ? -1 : this.claveRed.compareTo(that.objectField); | |
return comp; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment