javanattablenebulaglazedlists

How to sort a TreeList with a different comparator for each column in a NatTable


I'm using NatTable to display a tree with multiple columns. The tree is flattened into a SortedList which is used to create TreeList.

EventList<Person> eventList = GlazedLists.eventList(perfStats.getFlattenedTree());
TransformedList<Person, Person> rowObjectsGlazedList = GlazedLists.threadSafeList(eventList);
SortedList<Person> sortedList = new SortedList<(rowObjectsGlazedList, null);
TreeList treeList = new TreeList(sortedList, treeFormat, TreeList.nodesStartCollapsed());

This works to display the tree. However, now my issue is how do I sort this properly?

The desired outcome would be for the roots to be properly sorted, then the children inside properly sorted independently and so on.

Right now, I'm using the GlazedListsSortModel and it sorts the flattened tree then builds the display from that which does not work.

Any help or just pointing me in the right direction would be appreciated!


Solution

  • When using a TreeList you pass in a Comparator for the tree structure via TreeList#Format. This is needed to ensure that the tree is created properly, as it is derivated from a List. So even if there is a sorting applied via SortedList, the Comparator of the TreeList#Format will win in the end.

    To solve your requirement you therefore need to implement a TreeList#Format that takes the column sorting into account. This can be done by using the NatTable SortableTreeComparator for example. You can have a look at our TreeGridExample to get an idea of how this could look like.

    A more extended version can be seen in the GroupByComparator that is used to support sorting by column with the GroupBy feature.