Sort tree view nodes

1

In my tree view I have several child nodes. All nodes are created with numbers because they identify a different file and are composed of the date.

How could I order these nodes numerically? That is, type:

Raiz
    1
    2
    3
    4

And so on ...

Can anyone tell me if this is possible?

    
asked by anonymous 19.11.2015 / 17:00

1 answer

2

Yes, it is possible.

You should create your own Comparer to and set it to property value TreeViewNodeSorter of TreeView

public class NodeSorter : System.Collections.IComparer
{
    public int Compare(object x, object y)
    {
        TreeNode treeNode1 = (TreeNode)x;
        TreeNode treeNode2 = (TreeNode)y;    

        string.Compare(treeNode1.Text, treeNode2.Text);
    }
}

minhaTreeView.TreeViewNodeSorter = new NodeSorter();

And when you need to reorder the nodes

minhaTreeView.Sort();
    
19.11.2015 / 20:27