Solved

Sorting TreeView Nodes.

Posted on 2007-03-19
3
3,829 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: 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

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…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

728 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