我在我的java程序中使用了一个链表,该元素是一个自定义类型,它有三个字段,其中一个是Integer类型.我的问题是:如何根据整数提交的值对链表进行排序?
您可以将Collections.sort方法与自定义Comparator一起使用.
Collections.sort(your_list, new Comparator<YoureValueType>(){
@Override
public int compare(YoureValueType o1, YoureValueType o2){
if(o1.getMagicInt() < o2.getMagicInt()){
return -1;
}
if(o1.getMagicInt() > o2.getMagicInt()){
return 1;
}
return 0;
}
});
Run Code Online (Sandbox Code Playgroud)
编辑:我刚看到亚历山大评论waldheinz答案的非常大和小的价值.我更新了我的代码以反映他的论点.
| 归档时间: |
|
| 查看次数: |
13587 次 |
| 最近记录: |