This is the right way to compare strings:
int studentCompare = this.lastName.compareTo(s.getLastName());
This won't even compile:
if (this.getLastName() < s.getLastName())
Use
if (this.getLastName().compareTo(s.getLastName()) < 0) instead.
So to compare fist/last name order you need:
int d = getFirstName().compareTo(s.getFirstName());
if (d == 0)
d = getLastName().compareTo(s.getLastName());
return d;
Answer from Eugene Retunsky on Stack OverflowThis is the right way to compare strings:
int studentCompare = this.lastName.compareTo(s.getLastName());
This won't even compile:
if (this.getLastName() < s.getLastName())
Use
if (this.getLastName().compareTo(s.getLastName()) < 0) instead.
So to compare fist/last name order you need:
int d = getFirstName().compareTo(s.getFirstName());
if (d == 0)
d = getLastName().compareTo(s.getLastName());
return d;
The compareTo method is described as follows:
Compares this object with the specified object for order. Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.
Let's say we would like to compare Jedis by their age:
class Jedi implements Comparable<Jedi> {
private final String name;
private final int age;
//...
}
Then if our Jedi is older than the provided one, you must return a positive, if they are the same age, you return 0, and if our Jedi is younger you return a negative.
public int compareTo(Jedi jedi){
return this.age > jedi.age ? 1 : this.age < jedi.age ? -1 : 0;
}
By implementing the compareTo method (coming from the Comparable interface) your are defining what is called a natural order. All sorting methods in JDK will use this ordering by default.
There are ocassions in which you may want to base your comparision in other objects, and not on a primitive type. For instance, copare Jedis based on their names. In this case, if the objects being compared already implement Comparable then you can do the comparison using its compareTo method.
public int compareTo(Jedi jedi){
return this.name.compareTo(jedi.getName());
}
It would be simpler in this case.
Now, if you inted to use both name and age as the comparison criteria then you have to decide your oder of comparison, what has precedence. For instance, if two Jedis are named the same, then you can use their age to decide which goes first and which goes second.
public int compareTo(Jedi jedi){
int result = this.name.compareTo(jedi.getName());
if(result == 0){
result = this.age > jedi.age ? 1 : this.age < jedi.age ? -1 : 0;
}
return result;
}
If you had an array of Jedis
Jedi[] jediAcademy = {new Jedi("Obiwan",80), new Jedi("Anakin", 30), ..}
All you have to do is to ask to the class java.util.Arrays to use its sort method.
Arrays.sort(jediAcademy);
This Arrays.sort method will use your compareTo method to sort the objects one by one.
compareTo(Object o) method problem
Java CompareTo Sorting - Sorting Multiple Fields for Same Object
Can somebody help me with java (compareto()???)
[Java] Need help with a Generic compareTo
Videos
I need my class to implement the Comparable interface, but I can't get the compareTo() method to work. The error I'm getting is "The method compareTo(Process) of type Process must override or implement a supertype method"
The class creates Process objects.
@Override
public int compareTo(Process p2) {
return this.getPriority() - p2.getPriority();
}Obviously if I remove the @Override it works, but then I have an error that I need to add unimplemented methods. How do you fix this? I tried casting the Process as an Object but that didn't work.
I have a vague idea of what it does. It returns -1,0,1 depending on the circumstances.
But this is what I don't get. If you're comparing strings, how does it behave? I'm not even sure how the code looks like.. It doesn't make sense to compare strings.
How does the ordering work for this function if you're comparing a string? Is it the length of the string?
I just don't understand it.
Also taking a look at the following website and scrolling down:
http://javarevisited.blogspot.ca/2011/11/how-to-override-compareto-method-in.html
we see a "Student" class. Specifically paying attention to :
"return (this.id < otherStudent.id ) ? -1: (this.id > otherStudent.id) ? 1:0"
What are the following strings of symbols trying to say? I don't understand any of it =/. Can somebody break it down? Where did the ? -1: come from and what does it mean?
Same with the ? 1:0
Also if we look at the same page, you'll see another class below the "Student" class.
This time, it's the same student class but implements comparable<student>. Why does the compareTo() Method return the following value? "return age - otherStudent.age;"