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

public IComparer TreeViewNodeSorter { get; set; }
/** @property */
public IComparer get_TreeViewNodeSorter ()

/** @property */
public void set_TreeViewNodeSorter (IComparer value)

public function get TreeViewNodeSorter () : IComparer

public function set TreeViewNodeSorter (value : IComparer)

Not applicable.

Property Value

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(ty.Text, tx.Text);

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0