按字典顺序对ArrayList <node>进行排序?</node>

时间:2012-05-23 21:19:48

标签: java sorting collections arraylist

我想对具有Node类型的ArrayList进行排序,如下所示:

 [[country1: null], [name2: null], [city3: null], [region4: null], [name6: null]]

要获取Node名称的值,我使用函数getNodeName()so

 ArrayNode.get(0).getNodeName()  // return country1

我正在研究 collection.sort ,但我不知道我会怎么做,请提前感谢你。

3 个答案:

答案 0 :(得分:3)

我想你想要java.util.Comparator。您可以将它与Collections.sort

一起使用

答案 1 :(得分:3)

创建按节点名称排序的Comparator,然后Collections.sort(list,comparator)

答案 2 :(得分:3)

使用Comparator对象来定义比较逻辑。

类似的东西:

ArrayList<Node> nodes;
        Collections.sort(nodes, new Comparator<Node>() {
            @Override
            public int compare(Node o1, Node o2) {
                return o1.getNodeName().compareTo(o2.getNodeName());
            }
        });