I'm familiar with standard comparisons using the Comparable interface, although today I'm having some trouble when I want to compare several different variables.
I basically want to implement a compareTo method that yields the result -1 only when the following if statement is true:
if (o.maxX > minX && o.maxY > minY && o.minZ < maxZ)
Although, I'm not sure if this is possible using a comparable, or I'm just not as familiar with it as it seems. Because when I try the approach
public int compareTo(IsoSprite o) {
if (o.maxX > minX && o.maxY > minY && o.minZ < maxZ){
return -1;
}else if(o.maxX < minX && o.maxY < minY && o.minZ > maxZ){
return 1;
}
return 0;
}
I receive the error "Comparison method violates its general contract!". I want to clarify that I don't need help with understanding what this error means, because I've read several questions about it. Although, I still can't put my mind around this particular problem, because the solutions to the other questions that I've read have been trivial.
I would really appreciate some help with this comparison, it would be a life saver. Any input is also well appreciated.
Edit: After testing around, I have got something that almost works (not in all cases), but I can't figure out why:
public int compareTo(IsoSprite o) {
if (o.maxX > minX && o.maxY > minY && o.minZ < maxZ) {
return -1;
} else if (o.maxX > minX && o.maxY > minY && o.minZ > maxZ) {
return 1;
}else if (o.maxX < minX && o.maxY > minY && o.minZ > maxZ) {
return 1;
}else if (o.maxX < minX && o.maxY < minY && o.minZ > maxZ) {
return 1;
}else if (o.maxX < minX && o.maxY > minY && o.minZ < maxZ) {
return 1;
}else if (o.maxX > minX && o.maxY < minY && o.minZ > maxZ) {
return 1;
}else if (o.maxX < minX && o.maxY < minY && o.minZ > maxZ) {
return 1;
}else if (o.maxX > minX && o.maxY < minY && o.minZ < maxZ) {
return 1;
}else if (o.maxX < minX && o.maxY > minY && o.minZ < maxZ) {
return 1;
}else if(this != o){
return 1;
}
return 0;
}