title: Sort the Objects with comparator logic in java
body: <p>I am trying to sort the list of objects with following parameters</p>
  
  <p>a.group the two list objects whose partId is same as id of other object</p>
  
  <p>b. Push any object whose partId is null to the end of the list.</p>
  
  <p>c. sort the grouped objects based on the count in ascending order, preference is to</p>
  
  <p>I. both the objects in the group should have less count</p>
  
  <p>II. then any object in group having less count than other groups.</p>
  
  <p>Below is the code I have so far,</p>
  
  <pre><code>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&lt;LstObj&gt; lstArr = Arrays.asList(lst);
  
          lstArr.sort(new Comparator&lt;LstObj&gt;() {
              @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";
      }
  
  
  }
  </code></pre>
  
  <p>Output for above code is :</p>
  
  <pre><code>[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]
  </code></pre>
  
  <p>But I am looking for output as:</p>
  
  <pre><code> [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]
  </code></pre>
  
  <p>Anybody have any idea where exactly I'm doing wrong? </p>