Solved

Sorting TreeView Nodes.

Posted on 2007-03-19
3
3,562 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

910 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now