DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world
Java Comparator Interface
// A quick example of how to sort two objects using Java's Comparator interface.
// The call to Collections.sort uses an anonymous inner function to define
// the comparison between two objects.
public String[] sortNodes(ArrayList<Node> nodes) {
Node[] sortedNodes = new Node[nodes.size()];
Collections.sort(nodes, new Comparator<Node>() {
public int compare(Node o1, Node o2) {
return o2.priority - o1.priority;
}
});
for (int i=0; i < nodes.size(); i++) {
sortedNodes [i] = nodes.get(i);
}
return sortedNodes ;
}
class Node{
public String name = null;
public int priority;
}




