0
    public int compareTo(T o){

      if (this.getId().charAt(0) < o.getId() ){
         return 1;
      }else{
         return -1;
      }

      //Id data = a, b, f, z
      //num data = 1, 2, 4, 8

      //i want to sort the list according to their id first. then also sort it with their 'num' after sorting with their id first
         //sort id first. sort according to num after
   }

}

I want to sort the list according to their id first. and then according to num. How would i..

webprogrammer
  • 2,393
  • 3
  • 21
  • 27
  • 4
    Does this answer your question? [Sort a List of objects by multiple fields](https://stackoverflow.com/questions/6850611/sort-a-list-of-objects-by-multiple-fields) – OH GOD SPIDERS Feb 14 '20 at 10:36
  • Does this answer your question? [Using Comparable for multiple dynamic fields of VO in java](https://stackoverflow.com/questions/16206629/using-comparable-for-multiple-dynamic-fields-of-vo-in-java) – Dinesh Shekhawat Feb 14 '20 at 10:43

1 Answers1

0

try the following snippet.

public int compareTo(Person o){

    int idResult=id.compareTo(o.getId();
    if (idResult == 0) {//same
        return Integer.compare(this.num, o.num);//compare numbers
    }
    return idResult;//else return resut of id comparison
}
akshaya pandey
  • 997
  • 6
  • 16