根据对象字段对 ArrayList 进行排序 [重复]

2023-12-07

可能的重复:
对联系人数组列表进行排序

我正在存储DataNode中的对象ArrayList. The DataNode类有一个名为的整数字段degree。 我想找回DataNodeNodeList 中的对象按升序排列degree。我该怎么做。

List<DataNode> nodeList = new ArrayList<DataNode>();

使用自定义比较器:

Collections.sort(nodeList, new Comparator<DataNode>(){
     public int compare(DataNode o1, DataNode o2){
         if(o1.degree == o2.degree)
             return 0;
         return o1.degree < o2.degree ? -1 : 1;
     }
});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

根据对象字段对 ArrayList 进行排序 [重复] 的相关文章

随机推荐