Skip to content

Instantly share code, notes, and snippets.

@arturotena
Created September 23, 2014 23:32
Show Gist options
  • Save arturotena/14d275f25fc8e710d70a to your computer and use it in GitHub Desktop.
Save arturotena/14d275f25fc8e710d70a to your computer and use it in GitHub Desktop.
One option to implement Comparable.compareTo()
// 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