I'm trying to create an Comparator on my custom type.
Type {
int state = 0; // can be 0 or 1
String name = ""; // can be anything
}
I'm trying to sort the list so, that at first, on the top of list are items with one state and on the bottom the other state.
And in second step, items in both section (section 1: state 0, section 2: state 1), are sorted alphabetically.
I've tried this:
@Override
public int compare(Type i1, Type i2) {
boolean a = i1.state == 1;
boolean b = i2.state == 1;
if (a && b) {
return i1.name.compareTo(i2.name);
} else if (a && !b) {
return 1;
} else {
return -1;
}
}
But item's get kind of randomized with each item state change.
List with these items:
name : A B C D E F G H I J K
state: 1 0 1 1 0 1 0 0 1 1 0
should be listed like this:
B 0
E 0
G 0
H 0
K 0
A 1
C 1
D 1
F 1
I 1
J 1
Have anybody an idea how to solve it? Thanks