I am trying to sort the data from a list ,the list has the field id has :
field id :field_38637
field id :field_38469
field id :field_38468
field id :field_38638
field id :field_38637
field id :field_38469
field id :field_38468
field id :field_38468
field id :field_38469
field id :field_38468
field id :field_38637
size of list data is 11,for the above list data i am doing a sort has below :
Collections.sort(copedppList, new Comparator() {
@Override
public int compare(Object o1, Object o2) {
Field oo1 = (Field ) o1;
Field oo2 = (Field ) o2;
if(oo1.getFieldId()!=oo2.getFieldId()){}
return 1;
else
return 0;
});
for (int i = 0; i < copedppList.size(); i++) {
Field pp = (Field) copedppList
.get(i);
System.out.println(pp.getFieldID());
}
After sort i am getting the list has
field_38468
field_38468
field_38468
field_38468
field_38468
field_38469
field_38469
field_38469
field_38637
field_38637
field_38637
Edited:
The similar field id’s are be grouped together after sort but it should be grouped as per the incoming list which starts with 38637,38469,38468 ,what is wrong here ?I want to maintain the order of incoming list.
You are not sorting in your comparator, only checking equality. This would probably work better:
(Assumes that getFieldID is a non null String)
ps: you don’t seem to be using generics, it would enable you to remove all those casts from your code.
EDIT
You actually want to group the items, not sort them. The easiest way would be to do that with 2 lists (not tested):