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.
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.
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.