Sorting in Windows Forms TreeView

25 Jun 20212 minutes to read

Sorting can be performed on the tree nodes using the Sort function and specifying the function to be performed on either the checkbox or tag or text values in ascending or descending order.

The Sort operation sorts only the level 1 nodes. To perform the function on the other levels of nodes, the SortWithChildNode property should be set to true. The sort function can be done based on the value type which can be specified using SortType to either the option of Checkbox or Tag or Text. The order in which the sort function must be performed can be specified using the SortOrder that holds the values of Ascending or Descending.

Property Table

TreeNodeAdv Properties Description
SortOrder The SortOrder property indicates the order of the sorting: Ascending,Descending, None.
SortType The SortType property indicates the field. Nodes will be sorted based on the type of sorting.
treeNode.SortOrder = System.Windows.Forms.SortOrder.Ascending;
treeNode.SortType = Syncfusion.Windows.Forms.Tools.TreeNodeAdvSortType.CheckBox;
treeNode.SortOrder = System.Windows.Forms.SortOrder.Ascending
treeNode.SortType = Syncfusion.Windows.Forms.Tools.TreeNodeAdvSortType.CheckBox

Comparing Options for Sorting

  • The CompareOptions property gives additional options of comparing the texts of the nodes.TreeViewAdv provides the following CompareOptions

    1. IgnoreCase

    2. IgnoreNonSpace

    3. IgnoreSymbols

    4. IgnoreType

    5. IgnoreWidth

    6. OrdinalIgnoreCase

    7. StringSort and Ordinal.

  • The Comparer property is an object that implements the IComparer interface. If you need to compare the nodes by some other field, create an object of this type, set it to the node and that node will use the object in comparing the sub nodes.

Property Table

TreeNodeAdv Properties Description
CompareOptions Indicates the compare options used in the sorting of the nodes.
Comparer Indicates the object which compares two nodes.
treeNodeAdv9.CompareOptions = System.Globalization.CompareOptions.IgnoreCase;
treeNodeAdv9.Comparer = null;
TreeNodeAdv9.CompareOptions = System.Globalization.CompareOptions.IgnoreCase;
TreeNodeAdv9.Comparer = Null

Output

See Also

How to sort root node alone (keep child nodes intact) ?