Export (0) Print
Expand All

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)

property IComparer^ TreeViewNodeSorter {
	IComparer^ get ();
	void set (IComparer^ value);

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.

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

            // 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);

    // Declare the TreeView. 
    TreeView^ initialTreeView;
    TextBox^ displayNodes;
    Button^ setSorter;

    void InitializeInitialTreeView()
        // Create the TreeView
        initialTreeView = gcnew TreeView();
        initialTreeView->Size = System::Drawing::Size(200, 200);

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

        // Handle the click event for the button.
        setSorter->Click += gcnew EventHandler(this, 

        // Create the root nodes.
        TreeNode^ docNode = gcnew TreeNode("Documents");
        TreeNode^ spreadSheetNode = gcnew 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. 
    void SetSorter_Click(Object^ sender, EventArgs^ e)
        initialTreeView->TreeViewNodeSorter = gcnew NodeSorter();

.NET Framework

Supported in: 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

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

© 2014 Microsoft