Click here to Skip to main content
12,697,779 members (27,672 online)
Click here to Skip to main content
Add your own
alternative version

Stats

26.9K views
3 bookmarked
Posted

ASP.NET TreeView Sort

, 11 Sep 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
Easy way to sort nodes in a TreeView using a recursive function.
A few days ago, I needed to sort the nodes of a tree view. The solutions that I found over the Internet did not please me, so I decided to write my own. This solution is a simple recursive function that sorts tree nodes in an alphabetic order.

Create your tree view and add your nodes:


TreeView mytree = new TreeView();
//add your nodes here

Then simply call the sort function with the main node as the argument:


sort(node);

Here is the recursive function:


private void sort(TreeNode node)
{
    foreach (TreeNode n in node.ChildNodes)
        sort(n);
    try
    {
        TreeNode temp = null;
        List<TreeNode> childs = new List<TreeNode>();
        while(node.ChildNodes.Count>0)
        {
            foreach (TreeNode n in node.ChildNodes)
                if (temp == null || n.Text[0] < temp.Text[0])
                    temp = n;
            node.ChildNodes.Remove(temp);
            childs.Add(temp);
            temp = null;
        }
        node.ChildNodes.Clear();
        foreach (TreeNode a in childs)
            node.ChildNodes.Add(a);
    }
    catch { }
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

brunofer2007
Software Developer (Senior) Martifer Inovação e Gestão
Portugal Portugal
No Biography provided

You may also be interested in...

Pro

Comments and Discussions

 
Questionincorrect comparison Pin
Member 1024692319-Nov-14 4:04
memberMember 1024692319-Nov-14 4:04 
Suggestioncompare full words Pin
Klimovec12-Apr-12 3:49
memberKlimovec12-Apr-12 3:49 
GeneralHeyy it worked for me. Currently i have a treeview which pop... Pin
rjkumar19879-Jan-12 3:27
memberrjkumar19879-Jan-12 3:27 
GeneralRe: just replace the logical comparison "n.Text[0] < temp.Text[0... Pin
brunofer200715-Jan-12 0:59
memberbrunofer200715-Jan-12 0:59 
General1-The datasource should be sort not the tree in asp.net. An... Pin
JoePatent13-Sep-11 9:03
memberJoePatent13-Sep-11 9:03 
1-The datasource should be sort not the tree in asp.net. And then you fill the tree...
2- recursive function should always be avoid in prod development. This could be the source of instability. You can't predict the number of iteration and consequently the resources that will be required to run your routine.
3- foreach (TreeNode n in node.ChildNodes)
if (temp == null || n.Text[0] < temp.Text[0])
temp = n;
This need explanation. As soon as your temp is null you should not even execute your for each loop. That's a waste of time.
4-Also, there is already existing sort algorithm to accomplish this without having to code yourself an average one like this one based on bubble sort algo.
GeneralRe: 1 - Sometimes a TreeView doesn't have necessarily a datasour... Pin
brunofer200714-Sep-11 1:36
memberbrunofer200714-Sep-11 1:36 
GeneralCould I ask, why can't you use the default TreeView Sort met... Pin
George Swan11-Sep-11 21:11
memberGeorge Swan11-Sep-11 21:11 
GeneralRe: Hello George, as you can see in the title, this solution is ... Pin
brunofer200711-Sep-11 23:21
memberbrunofer200711-Sep-11 23:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170118.1 | Last Updated 11 Sep 2011
Article Copyright 2011 by brunofer2007
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid