Sort ArrayList <Node> lexicographically?
I want to sort an ArrayList with type Node, for example:
[[country1: null], [name2: null], [city3: null], [region4: null], [name6: null]]
To get the Node name value, I use the getNodeName () function, therefore
ArrayNode.get(0).getNodeName() // return country1
I look at collection.sort , but I do not know how I will do this, thanks in advance.
+3
3 answers
Use Comparator to define comparison logic.
Something like that:
ArrayList<Node> nodes;
Collections.sort(nodes, new Comparator<Node>() {
@Override
public int compare(Node o1, Node o2) {
return o1.getNodeName().compareTo(o2.getNodeName());
}
});
+3