Sorting the tree nodes

The tree component allows you to sort the displayed nodes. By default, the tree adapter sorts nodes using an arbitrary order (see ARBITRARY_COMPARATOR) in which the same set of objects is always displayed in the same order.
You can specify your own order by providing an object that implements the Comparator interface before the nodes are created. The compare() method of the comparator object will be called for pairs of IlpObject instances in the data source to establish a sort order. Objects are ordered locally, that is, within their parent node.
To retrieve the current sort order, use the method getSortComparator.
All the objects are sorted again when a new comparator is set.
For convenient sorting based on the attribute values of the business objects, JViews TGO provides the predefined IlpAttributeComparator class as an implementation of the Comparator interface. This class sorts objects based on the values of one or more of their attributes.

How to sort tree nodes

The following code sample shows you how to use IlpAttributeComparator to implement the sorting of tree nodes. This sample is available in <installdir> /samples/framework/datasource-explorer where <installdir> is the directory where you have installed JViews TGO.
// Create a comparator to sort all the nodes in the tree
// Use the predefined class IlpAttributeComparator to sort
// The first sort parameter is directory, so directories appear before files
// then the 'name' attribute
// then the 'path' attribute, as name is sometimes null (for roots)
IlpAttributeComparator sorter =
     new IlpAttributeComparator(dataSource.fileClass.getAttribute("directory"),
          false);
      
sorter.setDirectionAndOrder(dataSource.fileClass.getAttribute("name"),2,true);
      
sorter.setDirectionAndOrder(dataSource.fileClass.getAttribute("path"),3,true);

// Set the sorter to the tree BEFORE setting the data source
treeComponent.getAdapter().setComparator(sorter);