Skip to content

Instantly share code, notes, and snippets.

@afaquejam
Created July 19, 2014 11:11
Show Gist options
  • Save afaquejam/8d8aed418898ed8f7fb2 to your computer and use it in GitHub Desktop.
Save afaquejam/8d8aed418898ed8f7fb2 to your computer and use it in GitHub Desktop.
Java Comparable Interface
public class CustomObject implements Comparable<CustomObject>{
private int value;
public int compareTo(CustomObject that) {
if(this.value < that.value)
return -1;
else if(this.value > that.value)
return 1;
else
return 0;
}
public static void main(String[] arguments) {
CustomObject objectOne = new CustomObject();
objectOne.value = 674;
CustomObject objectTwo = new CustomObject();
objectTwo.value = 6;
System.out.println(objectOne.compareTo(objectTwo));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment