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
}
}
我想先根据 ID 对列表进行排序。然后根据num。我会怎样..
最佳答案
尝试以下代码片段。
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
}
关于java - java中严格使用compareTo按2个条件排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60224588/