public int compareTo(Person p) {
int res = 1;
String personStr = p.getId();
String thisId = this.getId();
if(thisId.equals(personStr)){
res = 0;
}
else if(thisId.compareTo(personStr)){
res = -1;
}
return res;
}
A quite simple compareTo method I have implemented but I don't get the error message. The condition in the else if statemint gives me a message saying that it can't convert from int to boolean. I get that, but the thing is that I'm using netiher. I just want to compare 2 simple strings, why does this happen?
What you should notice is that the interface 'compareTo' is returning an int 'public int compareTo' being the sign
if statements rely on a boolean value, however you use thisId.compareTo(personStr) which will return an integer, just like the method you are creating.
Your first if statement is fine - 'equals' returns a boolean. However the second does not, it will likely return either a -1, a 0 or a 1.