I have Model class which contains
int rs;
String name;
I want to get rs > 8
ArrayList contains all data
Collections.sort(arreyList, new Comparator< Model >() {
public int compare(Model one, Model other) {
int a = one.getrs();
int b = other.getrs();
if (a > 8) {
if (a > b)
return -1;
else if (a < b)
return 1;
else
return 0;
}
return 0;
}
});
but i am getting wrong , and I want to add more filter like > , < , or only that then for string also.
if/else if/elsecan be replaced simply byInteger.compare(b, a).compare(a, b) == -compare(b, a)- this isn't, because you are only consideringa > 8, and notb > 8.rs > 8? Comparators can't do that - you need to filter the list first.