I am trying to sort the list of objects with following parameters
a.group the two list objects whose partId is same as id of other object
b. Push any object whose partId is null to the end of the list.
c. sort the grouped objects based on the count in ascending order, preference is to
I. both the objects in the group should have less count
II. then any object in group having less count than other groups.
Below is the code I have so far,
public class ListTTest {
public static void main(String[] args) {
LstObj lstObj1 = new LstObj("0:0:1", "1:0:1", 49);
LstObj lstObj2 = new LstObj("0:0:2", "1:0:2", 2);
LstObj lstObj3 = new LstObj("0:2:1", "1:2:1", 0);
LstObj lstObj4 = new LstObj("0:2:2", null , 0);
LstObj lstObj5 = new LstObj("0:2:3", "1:2:3" , 2);
LstObj lstObj6 = new LstObj("0:2:4", "1:2:4" , 49);
LstObj lstObj7 = new LstObj("1:0:1", "0:0:1" , 49);
LstObj lstObj8 = new LstObj("1:0:2", "0:0:2" , 49);
LstObj lstObj9 = new LstObj("1:2:1", "0:2:1" , 0);
LstObj lstObj10 = new LstObj("1:2:2", null , 0);
LstObj lstObj11 = new LstObj("1:2:3", "0:2:3" , 49);
LstObj lstObj12 = new LstObj("1:2:4", "0:2:4" , 49);
LstObj lst[] = new LstObj[]{lstObj1,lstObj2,lstObj3,lstObj4,lstObj5,lstObj6,lstObj7,lstObj8,lstObj9,lstObj10,lstObj11,lstObj12};
List<LstObj> lstArr = Arrays.asList(lst);
lstArr.sort(new Comparator<LstObj>() {
@Override
public int compare(LstObj o1, LstObj o2) {
if(o1.partId==null){
return 1;
}else if(o2.partId==null){
return -1;
}else{
return -1*(o1.partId.compareTo(o2.id)-(o1.count-o2.count));
}
}
});
System.out.println(lstArr);
}
}
class LstObj {
String partId;
String id;
int count;
public LstObj( String id,
String partId,
int count
) {
this.count = count;
this.partId = partId;
this.id = id;
}
public String getPartId() {
return partId;
}
public String getId() {
return id;
}
public int getCount() {
return count;
}
public void setPartId(String partId) {
this.partId = partId;
}
public void setId(String id) {
this.id = id;
}
public void setCount(int count) {
this.count = count;
}
@Override
public String toString() {
return "LstObj [partId=" + partId + ", id=" + id + ", count=" + count
+ "]\n";
}
}
Output for above code is :
[LstObj [partId=1:2:1, id=0:2:1, count=0]
, LstObj [partId=1:2:3, id=0:2:3, count=2]
, LstObj [partId=0:2:1, id=1:2:1, count=0]
, LstObj [partId=1:0:2, id=0:0:2, count=2]
, LstObj [partId=1:2:4, id=0:2:4, count=49]
, LstObj [partId=0:2:3, id=1:2:3, count=49]
, LstObj [partId=1:0:1, id=0:0:1, count=49]
, LstObj [partId=0:0:1, id=1:0:1, count=49]
, LstObj [partId=0:0:2, id=1:0:2, count=49]
, LstObj [partId=0:2:4, id=1:2:4, count=49]
, LstObj [partId=null, id=0:2:2, count=0]
, LstObj [partId=null, id=1:2:2, count=0]
But I am looking for output as:
[LstObj [partId=0:2:1, id=1:2:1, count=0]
, LstObj [partId=1:2:1, id=0:2:1, count=0]
, LstObj [partId=0:0:2, id=1:0:2, count=2]
, LstObj [partId=1:0:2, id=0:0:2, count=49]
, LstObj [partId=0:2:3, id=1:2:3, count=2]
, LstObj [partId=1:2:3, id=0:2:3, count=49]
, LstObj [partId=0:0:1, id=1:0:1, count=49]
, LstObj [partId=1:0:1, id=0:0:1, count=49]
, LstObj [partId=0:2:4, id=1:2:4, count=49]
, LstObj [partId=1:2:4, id=0:2:4, count=49]
, LstObj [partId=null, id=0:2:2, count=0]
, LstObj [partId=null, id=1:2:2, count=0]
Anybody have any idea where exactly I'm doing wrong?
comparing(),thenComparing()andnullsLast(). I believe they can simplify your code quite a bit.o1.partId.compareTo(o2.id)can’t be correct. Further, combining two comparison results via minus like witho1.partId.compareTo(o2.id)-(o1.count-o2.count)makes no sense, as the magnitude of the value returned bycompareTois entirely unspecified (and what’s the supposed result anyway). Further,-1*(…)is a verbose way of flipping the sign (-(…)would do as well), which is a broken way of reversing a comparator. If the comparator evaluates toInteger.MIN_VALUE, flipping the sign causes an overflow, resulting inInteger.MIN_VALUEagain…