Created
July 19, 2014 11:11
-
-
Save afaquejam/8d8aed418898ed8f7fb2 to your computer and use it in GitHub Desktop.
Java Comparable Interface
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 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