How to sort a list of objects according to two parameters to compare at Java?
I have a class like that:
public class Zern extends Something{
private int costA;
private int costB;
public int getcostA() {
return costA;
}
public void setcostA(int costA) {
this.costA = costA;
}
public int getcostB() {
return costB;
}
public void setcostB(int costB) {
this.costB = costB;
}
}
I have a list that holds that kind of objects:
private List<Zern> zerns = new ArrayList<Zern>(MAX_ZERN_SIZE);
I will add new objects to my list however I always want to have a ordered list according to cost a and if there is an object at list which has the same cost with my object that I want to add I want to add that object according to their costB.
I mean:
Index of objects at list 0 1 2 3 4 5
CostA 10 15 22 22 25 36
CostB 26 12 17 19 23 44
If I want to add an object that has a costA 22 and costB 18,
it will locate at index 3.
How can I do it effectively (because I will add an object to a sorted list so it means that I can use binary search - if it i开发者_C百科s possible I want to find a solution according to that) with Comparator or something like that?
Use Collections.sort
with the following comparator:
Collections.sort(zerns, new Comparator<Zern>() {
@Override
public int compare(Zern z1, Zern z2) {
if (z1.getcostA() == z2.getcostA()) {
return z1.getcostB() == z2.getcostB() ? 0 :
z1.getcostB() < z2.getcostB() ? -1 : 1;
} else {
return z1.getcostA() < z2.getcostA() ? -1 : 1;
}
}
});
Update: If you do not need indexed access to your items you may want to use a sorted set implementation from the first place with a custom comparator:
TreeSet<Zern> zerns = new TreeSet<Zern>(new Comparator<Zern>() {
@Override
public int compare(Zern z1, Zern z2) {
if (z1.getcostA() == z2.getcostA()) {
return z1.getcostB() == z2.getcostB() ? 0 :
z1.getcostB() < z2.getcostB() ? -1 : 1;
} else {
return z1.getcostA() < z2.getcostA() ? -1 : 1;
}
}
});
Now objects can be added and your set will always remain sorted (note: I added a constructor and toString to your Zern class):
zerns.add(new Zern(10, 26));
System.out.println(zerns); // => [(10,26)]
zerns.add(new Zern(22, 19));
System.out.println(zerns); // => [(10,26), (22,19)]
zerns.add(new Zern(22, 17));
System.out.println(zerns); // => [(10,26), (22,17), (22,19)]
zerns.add(new Zern(15, 12));
System.out.println(zerns); // => [(10,26), (15,12), (22,17), (22,19)]
You can remove an item
zerns.remove(new Zern(22, 17));
System.out.println(zerns); // => [(10,26), (15,12), (22,19)]
or remove the worst cost item
zerns.remove(zerns.last());
System.out.println(zerns); // => [(10,26), (15,12)]
or get the best cost item via
System.out.println(zerns.first()); // => (10,26)
Just compare the first criteria. If they match, compare the second criteria:
public int compareTo(Zern other) {
final int result;
if (this.costA == other.costA) {
if (this.costB > other.costB) {
result = 1;
} else if (this.costB < other.costB) {
result = -1;
} else {
result = 0;
}
} else {
if (this.costA > other.costA) {
result = 1;
} else if (this.costA < other.costA) {
result = -1;
} else {
result = 0;
}
}
return result;
}
精彩评论