If you want the sort result to be the only list sorted by date, you must put all the nodes in one list in the array. If two types (node1 and node2) extend a common base class, you can use Java Generics for your list.
List<Node> nodes = new ArrayList<Node>(); nodes.add(node1); nodes.add(node2); Node[] nodeArrayToSort = nodes.toArray();
If the two types of node are not inherited from the general class, you can simply use the List of objects.
Now you have to write your own comparator. here is an example of what you could use if node types have a common superclass that contains a Date field.
public class NodeComparator implements Comparator<Node> { @Override public int compare(Node node1, Node node2) { return node1.getDate().compare(node2.getDate()); } }
Now that you have your custom comparator and your array with all your nodes, this is one line of Java code to sort the list.
Arrays.sort(nodeArrayToSort, new NodeComparator());
The javadoc for the above method can be found here if you want more information about its behavior.
Using the above method, it's easy to see how you could write any comparison function to change the behavior of your type. You can also write as many custom Comparator classes as you want so you can switch them at runtime. Hope this helps! :)
Jesse webb
source share