java - Using comparable to compare different variables -
i'm familiar standard comparisons using comparable interface, although today i'm having trouble when want compare several different variables.
i want implement compareto method yields result -1 when following if statement true:
if (o.maxx > minx && o.maxy > miny && o.minz < maxz)
although, i'm not sure if possible using comparable, or i'm not familiar seems. because when try 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 error "comparison method violates general contract!". want clarify don't need understanding error means, because i've read several questions it. although, still can't put mind around particular problem, because solutions other questions i've read have been trivial.
i appreciate comparison, life saver. input appreciated.
edit: after testing around, have got works (not in cases), 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; }
if using sort objects, comparable or comparator must behaved , must respect transitive , other rules. if not, code broken. recommended comparing methods consistent equals, means a.compareto(b) returns 0 if , if a.equals(b), unlike stated in deleted answer, not absolute requirement.
your code still breaks commutative rule comparing, namely
a.compareto(b) == -b.compareto(a)
let's @ rules:
public int compareto(isosprite o) { // comparison 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 }else if (o.maxx < minx && o.maxy > miny && o.minz < maxz) { return 1; // comparison j }else if(this != o){ return 1; } return 0; }
look @ "comparison c":
}else if (o.maxx < minx && o.maxy > miny && o.minz > maxz) {
which returns 1. converse of should return -1, doesn't since find converse in "comparison h":
}else if (o.maxx > minx && o.maxy < miny && o.minz < maxz) {
but returns 1. here (and possibly elsewhere) a.compareto(b) not equal - of b.compareto(a).
Comments
Post a Comment