Sorting
Flexible TreeView supports the following sorting modes:
- By columns and node controls bound data in the treeview with columns.
- By node controls bound data in the treeview without columns.
Sorting by columns and node controls bound data#
If treeview contains columns, then sorting will follow these rules:
- For every column with SortOrder property not equal to eSortOrder.None, get list of node controls that shown in this column with enabled Sortable property.
- Get the selected node controls' bound data and sort it in the order defined in the column SortOrder property.
Automatic re-sorting will take place when:
- Column's SortOrder property has changed.
- Options.Sorting.AutoReSort treeview property is enabled and treeview structure (column or node control was added or removed) or content (node was added or removed or its data has changed) has changed.
Sorting by node controls bound data#
If treeview doesn't contain columns, then sorting will follow these rules:
- Get list of node controls with Sortable property enabled.
- Get those node controls' bound data and sort it in ascending order. To change the default, ascending, sort order, call the Sort method manually over the treeview.Root or particular node and pass in the custom sort order.
Automatic re-sorting will take place when:
- Options.Sorting.AutoReSort treeview property is enabled and treeview structure (column or node control was added or removed) or content (node was added or removed or its data has changed) has changed.
Custom sorting#
Flexible TreeView allows two ways of custom sorting:
- Changing default nodes compare algorithm while sorting algorithm isn't affected.
- Completely manual implementation of sorting algorithm.
Changing default nodes compare algorithm#
When the treeview is sorting nodes, it uses a callback method, defined in the treeview's DefaultNodeComparer property, that is intended to compare two nodes. You can change it to your implementation to change nodes compare logic.
Example
tree.DefaultNodeComparer = MyComparer;
tree.ReSort();
// new node comparer where we move all checked nodes on top.
eCompareResult MyComparer(Node node1, Node node2, SortContext context)
{
if (node1.CheckState == node2.CheckState)
return eCompareResult.Equal;
// move all checked nodes on top.
if (node1.CheckState == eCheckState.Checked)
return eCompareResult.Higher;
if (node2.CheckState == eCheckState.Checked)
return eCompareResult.Lower;
// use default comparer's result.
return context.DefaultComparer(node1, node2, context);
}
tree.DefaultNodeComparer = AddressOf MyComparer
tree.ReSort()
' new node comparer where we move all checked nodes on top.
Private Function MyComparer(node1 As Node, node2 As Node, context As SortContext) As eCompareResult
If node1.CheckState = node2.CheckState Then
Return eCompareResult.Equal
End If
' move all checked nodes on top.
If node1.CheckState = eCheckState.Checked Then
Return eCompareResult.Higher
End If
If node2.CheckState = eCheckState.Checked Then
Return eCompareResult.Lower
End If
' use default comparer's result.
Return context.DefaultComparer(node1, node2, context)
End Function
Manual sorting#
Flexible TreeView allows you to completely replace the default sorting logic, allowing a programmer to implement it manually. To do that, just enable the Options.Sorting.ManualSort treeview property and subscribe to the ColumnSortOrderChanged treeview event where you need to implement your sorting algorithm.
Example
Separate nodes sorting#
While Flexible TreeView supports sorting of all treeview nodes, it also allows you to sort any particular node by calling the Sort node method where you can pass the desired sort order and custom nodes comparer callback.
Groups always on top#
In some cases, you need to keep nodes (so-called ‘groups') on top of the treeview even after sorting. To do that, enable the Options.Sorting.GroupsAlwaysOnTop treeview property and subscribe to the IsGroupNodes treeview event to define which node is a group and should remain before all other nodes.
Example
tree.Options.Sorting.GroupsAlwaysOnTop = true;
tree.IsGroupNodes += tree_IsGroupNodes;
void tree_IsGroupNodes(FlexibleTreeView treeview, GroupNodeEventArgs args)
{
// move all checked nodes on top.
if (args.Node1.CheckState == eCheckState.Checked)
args.IsNode1Group = true;
if (args.Node2.CheckState == eCheckState.Checked)
args.IsNode2Group = true;
}
tree.Options.Sorting.GroupsAlwaysOnTop = True
tree.IsGroupNodes += tree_IsGroupNodes
Private Sub tree_IsGroupNodes(treeview As FlexibleTreeView, args As GroupNodeEventArgs)
' move all checked nodes on top.
If args.Node1.CheckState = eCheckState.Checked Then
args.IsNode1Group = True
End If
If args.Node2.CheckState = eCheckState.Checked Then
args.IsNode2Group = True
End If
End Sub
Node post compare audit#
If you need to fine-tune the nodes compare result, but you don't want to completely implement the sorting algorithm, you can use the node post-compare audit mechanism. It allows you to call your code when two nodes have been compared, but haven't moved yet, so you can change the compare result. To do that, enable the Options.Sorting.NodePostCompareAudit treeview property and subscribe to the NodePostCompareAudit treeview event where you can change the two nodes compare result.
Example
tree.Options.Sorting.NodePostCompareAudit = true;
tree.NodePostCompareAudit += tree_NodePostCompareAudit;
void tree_NodePostCompareAudit(FlexibleTreeView treeview, NodeCompareEventArgs args)
{
// move higher the nodes with bigger child nodes count
if (args.Node1.Nodes.Count > args.Node2.Nodes.Count)
{
args.CompareResult = (args.SortOrder == SortOrder.Ascending) ? eCompareResult.Higher : eCompareResult.Lower;
}
}
tree.Options.Sorting.NodePostCompareAudit = True
tree.NodePostCompareAudit += tree_NodePostCompareAudit
Private Sub tree_NodePostCompareAudit(treeview As FlexibleTreeView, args As NodeCompareEventArgs)
' move higher the nodes with bigger child nodes count
If args.Node1.Nodes.Count > args.Node2.Nodes.Count Then
args.CompareResult = If((args.SortOrder = SortOrder.Ascending), eCompareResult.Higher, eCompareResult.Lower)
End If
End Sub