I am familiar with standard comparisons using the Comparable interface, although today I am having problems when I want to compare several different variables.
Basically, I want to implement the compareTo method, which gives the result -1 only if the following if if is executed:
if (o.maxX > minX && o.maxY > minY && o.minZ < maxZ)
Although, I'm not sure that this is possible using comparable ones, 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 get the error message "The comparison method violates the general contract!". I want to clarify that I do not need help understanding this error because I read a few questions about it. Although, I still canβt deal with this particular problem, because the solutions to other issues that I read were trivial.
I would really appreciate help in this comparison, it would be a life saver. Any input is also well appreciated.
Edit: after testing, I have something that almost works (not in all cases), but I cannot understand 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; }
source share