title: ConcurrentSkipListMap Sorting Using Comparator
body: <p>I am using 'ConcurrentSkipListMap' (Since my environment will be multythreaded) and a 'Comparator' to sort the inserting object according to its Id &amp; date. in the 'TestObject' its Name will be unique. so i used it as my 'key' in the map. 'Id' will be a arbitrary value. i need to sort my map according to the 'Id' and 'date' values.(if Id s are equal will sort it by date) for here i just add current date and gave the focus on the ID field. But map didn't return me the sorting order i expect.  </p>
  
  <pre><code>public class SortTest {
  
  private static final Comparator TEST_COMPARATOR = new TestComparator();
  private static Map&lt;String, TestObject&gt; map = new ConcurrentSkipListMap&lt;String, TestObject&gt;();
  
  private static class TestComparator&lt;T&gt; implements Comparator&lt;TestObject&gt; {
  
      @Override
      public int compare(TestObject o1, TestObject o2) {
          Integer x1 = o1.getId();
          Integer x2 = o2.getId();
  
          int Comp = x1.compareTo(x2);
  
          if (Comp != 0) {
              return Comp;
          } else {
  
              Date d1 = o1.getDate();
              Date d2 = o2.getDate();
  
              return d1.compareTo(d2);
          }
      }
  
  }
  
  public static void construct() {
  
      for (int i = 1; i &lt;= 10; i++) {
          TestObject t = new TestObject();
          t.setId(i%3);
          t.setDate(new Date());
          t.setName("Obj_"+i);
          System.out.println(t);
          map.put(t.getName(),t);
  
  
      }
  
  }
  public static void main(String[] args) {
      SortTest x = new SortTest();
      x.construct();
      System.out.println(map);
  }
  </code></pre>
  
  <p>}</p>
  
  <p>And the ObjectClass is - </p>
  
  <pre><code>public class TestObject {
  
  private String name;
  
  private int id;
  
  Date date;
  
  public int getId() {
      return id;
  }
  
  public void setId(int id) {
      this.id = id;
  }
  
  public Date getDate() {
      return date;
  }
  
  public void setDate(Date date) {
      this.date = date;
  }
  
  public String getName() {
      return name;
  }
  
  public void setName(String name) {
      this.name = name;
  }
  
  /* (non-Javadoc)
   * @see java.lang.Object#hashCode()
   */
  @Override
  public int hashCode() {
      final int prime = 31;
      int result = 1;
      result = prime * result + ((name == null) ? 0 : name.hashCode());
      return result;
  }
  
  
  
  /* (non-Javadoc)
   * @see java.lang.Object#toString()
   */
  @Override
  public String toString() {
      return "TestObject [name=" + name + ", id=" + id + "]";
  }
  
  /* (non-Javadoc)
   * @see java.lang.Object#equals(java.lang.Object)
   */
  @Override
  public boolean equals(Object obj) {
      if (this == obj)
          return true;
      if (obj == null)
          return false;
      if (getClass() != obj.getClass())
          return false;
      TestObject other = (TestObject) obj;
      if (name == null) {
          if (other.name != null)
              return false;
      } else if (!name.equals(other.name))
          return false;
      return true;
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Out snippet is -</p>
  
  <blockquote>
    <p>{Obj_1=TestObject [name=Obj_1, id=1], Obj_10=TestObject [name=Obj_10, id=1], Obj_2=TestObject [name=Obj_2, id=2], Obj_3=TestObject [name=Obj_3, id=0],......</p>
  </blockquote>
  
  <p>Expected Order is - </p>
  
  <p>Obj_3(Since Id =0 ),Obj_1,Obj_10, (Ids are =1 ), Obj_2 (Id =2)</p>
  
  <p>Can anyone please point me out what i am doing wrong here? Thanks in advance.</p>