Using comparable to compare different variables

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; } 
+5
source share
1 answer

If you use this to sort your objects, then your Comparable or Comparator must behave well and must comply with transitional and other rules. If not, then your code is broken. It is recommended that your comparison methods be consistent with peers, which means that a.compareTo (b) returns 0 if and only if a.equals (b), but unlike the remote answer indicated, this not an absolute requirement.

Your code still breaks the commutative, as a rule, comparison, namely:

 a.compareTo(b) == -b.compareTo(a) 

Look at your rules:

 public int compareTo(IsoSprite o) { // Comparison A if (o.maxX > minX && o.maxY > minY && o.minZ < maxZ) { return -1; // Comparison B } else if (o.maxX > minX && o.maxY > minY && o.minZ > maxZ) { return 1; // Comparison C }else if (o.maxX < minX && o.maxY > minY && o.minZ > maxZ) { return 1; // Comparison D }else if (o.maxX < minX && o.maxY < minY && o.minZ > maxZ) { return 1; // Comparison E }else if (o.maxX < minX && o.maxY > minY && o.minZ < maxZ) { return 1; // Comparison F }else if (o.maxX > minX && o.maxY < minY && o.minZ > maxZ) { return 1; // Comparison G }else if (o.maxX < minX && o.maxY < minY && o.minZ > maxZ) { return 1; // Comparison H }else if (o.maxX > minX && o.maxY < minY && o.minZ < maxZ) { return 1; // Comparison I }else if (o.maxX < minX && o.maxY > minY && o.minZ < maxZ) { return 1; // Comparison J }else if(this != o){ return 1; } return 0; } 

Take a look at Comparison C:

 }else if (o.maxX < minX && o.maxY > minY && o.minZ > maxZ) { 

which returns 1. The inverse of this should return -1, but this is not so, since we find the opposite in "Comparison H":

 }else if (o.maxX > minX && o.maxY < minY && o.minZ < maxZ) { 

But it also returns 1. Thus, here (and possibly elsewhere) a.compareTo (b) is not equal to - b.compareTo (a).

+2
source

All Articles