Solved

Sorting TreeView Nodes.

Posted on 2007-03-19
3
3,723 Views
Last Modified: 2007-12-19
I want to sort my TreeView alphabetically.

So all the Nodes, and all of their children are sorted alphabetically.

I have searched previous solutions and found this bubble search:


   public static void SortNodes(System.Windows.Forms.TreeNode _Node)
    {
      SortTreeNodes(_Node.Nodes);
    }

    public static void RecurseTree(System.Windows.Forms.TreeNodeCollection _Nodes, ProcessNodeDelegate FunctionToCall)
    {
      if (_Nodes == null || FunctionToCall == null)
      {
        return;
      }
      foreach (System.Windows.Forms.TreeNode Child in _Nodes)
      {
        RecurseTree(Child.Nodes, FunctionToCall);
        FunctionToCall(Child);
      }
    }
    public delegate void ProcessNodeDelegate(System.Windows.Forms.TreeNode _Node);

    public static void SortTreeView(System.Windows.Forms.TreeView _TreeView)
    {
      RecurseTree(_TreeView.Nodes, new ProcessNodeDelegate(SortNodes));
    }

    public static void SortTreeNodes(TreeNodeCollection _Nodes)
    {
      for (int i = 0; i < _Nodes.Count - 1; i++)
      {
        for (int j = _Nodes.Count - 1; j > i; j--)
        {
          if (((IComparable)_Nodes[j - 1].Tag).CompareTo(_Nodes[j].Tag) >= 0)
          {
            TreeNode tmp = _Nodes[j];
            _Nodes[j] = _Nodes[j - 1];
            _Nodes[j - 1] = tmp;
          }
        }
      }
    }
0
Comment
Question by:directxBOB
  • 2
3 Comments
 
LVL 3

Accepted Solution

by:
Kate12 earned 500 total points
ID: 18753771
you can use:

TreeView.Sort();

In case you want to control de sorting of the tree you'll have to use the TreeView property "TreeViewNodeSorter" which implements a IComparer.

treeView.TreeViewNodeSorter = new NodeSorter();

// Node sorter
class NodeSorter : IComparer
{
    public int Compare(object x, object y)
    {
        TreeNode nodeX = (TreeNode)x;
        TreeNode nodeY = (TreeNode)y;
        return nodeX.Text.CompareTo(nodeY.Text);
    }
}
HTH
0
 

Author Comment

by:directxBOB
ID: 18753906
Do I not require a cast for this?

treeView.TreeViewNodeSorter = new NodeSorter();
0
 
LVL 3

Expert Comment

by:Kate12
ID: 18753926
No casting is needed because the NodeSorter implements the IComparer.
But again, try using the TreeView.Sort() without creating a  custom TreeViewNodeSorter. Create a custom one if your sorting is more complex than the example I posted (NodeSorter).
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question