Solved

Sorting TreeView Nodes.

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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C# parent child form 5 28
C#.NET and microsoft certification. 3 40
HttpPostedFile 1 26
C# LINQ ForEach() question 6 28
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!
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

777 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