I have a Java ArrayList containing some objects of type ObjType.
Let's say the object has two fields:
- A boolean field isManual()
- A double value getAffinity()
I'm trying to order this ArrayList based on more than one criteria:
-First all the objects with isManual=true on the same order that they already have in the ArrayList
-Then I want all the objects with isManual=false ordered by the getAffinityValue (from the lowest to the greatest)
I've come up with this code, which is not working (it seems it's randomly sorting):
Collections.sort(coda, new Comparator<ObjType>() {
public int compare(ObjType a, ObjType b) {
boolean b1=a.isManual();
boolean b2=b.isManual();
if(b1 && b2) {
if (a.getAffinity() < b.getAffinity()) return 1;
if (a.getAffinity() > b.getAffinity()) return -1;
return 0;
}
if (b1) return -1;
if (b2) return 1;
if (a.getAffinity() < b.getAffinity()) return 1;
if (a.getAffinity() > b.getAffinity()) return -1;
return 0;
}
}
Any suggestions? Thanks a lot!