Sorting with GlazedLists

In this section we will walk through the SortableGridExample to demonstrate sorting in NatTable. Further on, we will see how to plugin custom comparators.

NatTable uses the SortedList from the GlazedLists project to do the actual sorting. Hence, as a first step, you will have to wrap your List data structure with a SortedList instance. The IDataProvider now uses the sorted list as its data source.

SortedList sortedList = new SortedList(eventList, null); IColumnPropertyAccessor columnPropertyAccessor = new ReflectiveColumnPropertyAccessor(propertyNames); bodyDataProvider = new ListDataProvider(sortedList, columnPropertyAccessor);

Snippet from GlazedListsGridLayer of SortableGridExample

The sorting functionality is added by the SortHeaderLayer. This implies that this layer has to be added to the stack to enable sorting. Since sorting is triggered by the column header this layer will be added to the column header layer stack.

columnHeaderLayer = new ColumnHeaderLayer( dataLayer, bodyLayerStack, bodyLayerStack.getSelectionLayer()); SortHeaderLayer sortHeaderLayer = new SortHeaderLayer( columnHeaderLayer, new GlazedListsSortModel( sortedList, columnPropertyAccessor, configRegistry, dataLayer), false);

Snippet from GlazedListsColumnHeaderLayerStack of SortableGridExample

In the above snippet, the SortHeaderLayer takes in a ISortModel (second parameter). This is the GlazedLists specific code to which we delegate the sorting. If you wish to use your custom implementation of a sort algorithm, you can plug in your own ISortModel here.

Also note that we set 'autoconfigure' (last parameter) to false. This instructs the layer not to use any default setup. We do this in order to enable us to modify the default sort configuration. More on that in a bit. You can set this to true if you are happy with the default settings.

Using custom comparators

The default comparator treats the objects in the column being sorted as Comparable.

Custom comparators can be plugged in by registering your comparator as a SortConfigAttributes.SORT_COMPARATOR attribute in the config registry.
This is a two step process:

  1. Apply labels to the column header cells
  2. Register your comparator against those labels

// Add label accumulator ColumnOverrideLabelAccumulator labelAccumulator = new ColumnOverrideLabelAccumulator(columnHeaderDataLayer); columnHeaderDataLayer.setConfigLabelAccumulator(labelAccumulator); // Register labels labelAccumulator.registerColumnOverrides( RowDataListFixture.getColumnIndexOfProperty(RowDataListFixture.RATING_PROP_NAME), CUSTOM_COMPARATOR_LABEL); // Register custom comparator configRegistry.registerConfigAttribute(SortConfigAttributes.SORT_COMPARATOR, getCustomComparator(), DisplayMode.NORMAL, CUSTOM_COMPARATOR_LABEL);
Disable sorting on selected columns

Disabling sorting is very similar to applying custom comparator to a column. The only difference is that you register a NullComparator as your custom comparator. The SortableGridExample demonstrates this.

Customize sort configuration

The DefaultSortConfiguration object sets up the:

  1. default sort comparators (uses Comparable.compareTo())
  2. default sort header cell painters (Arrows of varying shapes)
  3. default mouse bindings for triggering sorting (Alt + left click, Alt + Shift + left click for additive sort)

By overriding the DefaultSortConfiguration you can customize the settings mentioned above. An example of this is the SingleClickSortConfiguration.

This overrides the default sort configuration and changes the mouse bindings to sort on a single click. Remember to register your new configuration as follows:

sortHeaderLayer.addConfiguration(new SingleClickSortConfiguration());
Sorting without GlazedLists

It is recommended to use GlazedLists to get full performance benefits on using the NatTable with huge datasets. Nevertheless it is possible to enable sorting without using GlazedLists. To do this you have to implement your own ISortModel and use it for instantiating the SortHeaderLayer instead of using the GlazedListsSortModel.