这是我的数据集[B,A,D,C,3,10,1,2,11,14]
我想像这样排序[1,2,3,10,11,14,A,B,C,D]
当我使用以下内容
public class ArenaModel implements Comparable<ArenaModel>{
private String sectionName;
@Override
public int compareTo(ArenaModel o) {
return sectionName.compareTo(o.sectionName);
}
在主课中,我执行以下操作。
Collections.sort(arenaArrayList);
它确实排序,但单位数字没有排序,我得到以下结果。
[10,11,14,1,2,3,A,B,C,D]
最佳答案
试试这个:
public class ArenaModel implements Comparable<ArenaModel>{
private String sectionName;
@Override
public int compareTo(ArenaModel o) {
try {
Integer s1 = Integer.valueOf(sectionName);
Integer s2 = Integer.valueOf(o.sectionName);
return s1.compareTo(s2);
} catch (NumberFormatException e) {
// Not an integer
}
return sectionName.compareTo(o.sectionName);
}
}
关于java - Collections.sort() 不排序单个数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57163393/