TreeView.TreeViewNodeSorter Property

Note: This property is new in the .NET Framework version 2.0.

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)

Property Value

The IComparer to perform the custom sort.

The custom sort is applied when the TreeViewNodeSorter is set. When the TreeViewNodeSorter property is set, the Sorted property is automatically set to false.

After the TreeViewNodeSorter is set, if the Sorted property is set to true, the nodes will be sorted in alphabetical order. You can call the Sort method to sort the items again with the custom sorter specified by the TreeViewNodeSorter property.

There must be two or more root nodes in the TreeView for the custom sort to be 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 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 .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0

Community Additions