TreeList Sorting Specifics
- 2 minutes to read
Sorting Data Represented as a Tree-Like Structure
Basically, sorting a table's data by the values of a column results in arranging values of the column in either ascending or descending order. This is the standard method of sorting data for controls which display data in a simple tabular form. However, this cannot be implemented in the TreeList control, since in most cases, this may lead to destroying the tree-like data representation. So, the sorting algorithm used by the Tree List differs from the one described above.
Each row of the Tree List control has a nesting level within the tree. Rows are sorted with respect to their nesting levels, in order to preserve the tree-like structure. First, root level rows are sorted by values of a column. Note, that after this has been done, the root level rows are not arranged contiguously. They are separated from each other by their child rows. Immediate child rows of each root node are then sorted (note that each root node's children are sorted separately). This process is repeated for second level rows, and so on, until the entire hierarchy has been sorted. Once sorting is complete, the rows of each nesting level are sorted within their group. (A group is a set of rows having the same parent row).
The image below displays the original data within the Tree List control and its arrangement after sorting by values of a column.
Sorting Data by Values of Multiple Columns
The Tree List control is capable of sorting data by the values of multiple columns. Let's consider the case when two columns are involved in sorting. When sorting data by values of the first column, data rearrangement occurs as described above. The second column only affects sorting for records belonging to the same group with identical values, within the first column.
The following image displays the changes when the second column affects the sort. The Budget column is initially sorted. However, it contains the identical values within one group.So, including the Location column within the sort changes the order of the affected rows.
Note: sorting by a third column is only necessary when rows from a group contain identical values in the first two sort columns.