This documentation is archived and is not being maintained.

TreeView.TreeViewNodeSorter Property

Gets or sets the implementation of IComparer to perform a custom sort of the TreeView nodes.

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

public IComparer TreeViewNodeSorter { get; set; }

Property Value

Type: System.Collections.IComparer
The IComparer to perform the custom sort.

The custom sort is applied when the TreeViewNodeSorter is set.

If a TreeNode label is changed, you can call the Sort method to sort the items again with the custom sorter specified by the TreeViewNodeSorter property.


The default sorter uses the Compare method based on the CurrentCulture specified by the application. This means that TreeNode objects with equal value are kept in the order in which they were added to the TreeView control. This behavior may be different if a custom sort is applied.

The following code example demonstrates how to use the TreeViewNodeSorter property to sort nodes from smallest to largest. To run this example, paste the following code into a Windows Form and call InitializeTreeView1 from the form's constructor or Load event handler.

// Declare the TreeView. 
private TreeView treeView1;
private TextBox textBox1;
private Button button1;

private void InitializeTreeView1()
    // Create the TreeView
    treeView1 = new TreeView();
    treeView1.Size = new Size(200, 200);

    // Create the button and set some basic properties. 
    button1 = new Button();
    button1.Location = new Point(205, 138);
    button1.Text = "Set Sorter";

    // Handle the click event for the button.
    button1.Click += new EventHandler(button1_Click);

    // Create the root nodes.
    TreeNode docNode = new TreeNode("Documents");
    TreeNode spreadSheetNode = new TreeNode("Spreadsheets");

    // Add some additional nodes.

    // Add the root nodes to the TreeView.

    // Add the TreeView to the form.

// Set the TreeViewNodeSorter property to a new instance 
// of the custom sorter. 
private void button1_Click(object sender, EventArgs e)
    treeView1.TreeViewNodeSorter = new NodeSorter();

// Create a node sorter that implements the IComparer interface. 
public class NodeSorter : IComparer
    // Compare the length of the strings, or the strings 
    // themselves, if they are the same length. 
    public int Compare(object x, object y)
        TreeNode tx = x as TreeNode;
        TreeNode ty = y as TreeNode;

        // Compare the length of the strings, returning the difference. 
        if (tx.Text.Length != ty.Text.Length)
            return tx.Text.Length - ty.Text.Length;

        // If they are the same length, call Compare. 
        return string.Compare(tx.Text, ty.Text);

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0