This is a migrated thread and some comments may be shown as answers.

Sorting of Nodes after client add or drop

4 Answers 153 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Terry Webster
Top achievements
Rank 2
Terry Webster asked on 21 Feb 2008, 02:11 PM
I have an application in which we are using the treeview to add nodes to existing nodes that already have children.  When dropping or adding a node, the node that is added is appended to the end of the nodes collection.  I need to be able to sort the collection after add or drop alphabetically by the node text property.

Any ideas on how to do this?

I see that the ASP.NET treeview has a sort property that prometheus does not have.

Please advise...

Thanks,

Terry Webster
Chief Technology Architect
Highmark Technology

4 Answers, 1 is accepted

Sort by
0
Nikolay
Telerik team
answered on 22 Feb 2008, 05:03 PM
Hi Terry Webster,

The sort mechanism has been removed as we could not predict all the Sorting scenarios the users might require. Therefore, we decided to let the users sort the treeview in their very custom manner. We hope that this is acceptable for you.

Regards,
Nick
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
0
Terry Webster
Top achievements
Rank 2
answered on 25 Feb 2008, 04:36 PM
I strongly disagree with this decision.  Sorting should be at least supported on Text and/or Value.  Even better, give us a dataitem sort capability in which we can specify a value to sort on.  For example, just make it an order list where we provide you with an integer from 1-x for sorting the nodes at each level in the tree.

If we can't have the latter, i would at least consider putting alphanumeric by Text or Value.  If we don't use those, we can still sort however we wish ourselves....

Thanks,

Terry Webster
Chief Technology Architect
Highmark Technology
0
Nikolay
Telerik team
answered on 26 Feb 2008, 10:46 AM
Hi Terry Webster,

I believe that the following sorting approach would help you sort the TreeNodes:

protected void Page_Load(object sender, EventArgs e) 
    { 
        if (!Page.IsPostBack) 
        { 
            SortNodes(RadTreeView1.Nodes); 
        } 
    } 
 
    private void SortNodes(RadTreeNodeCollection collection) 
    { 
        Sort(collection); 
        foreach (RadTreeNode node in collection) 
        { 
            if (node.Nodes.Count > 0) 
            { 
                SortNodes(node.Nodes); 
            } 
        }         
    } 
 
    public void Sort(RadTreeNodeCollection collection) 
    { 
        RadTreeNode[] nodes = new RadTreeNode[collection.Count]; 
        collection.CopyTo(nodes, 0); 
        Array.Sort(nodes, new TreeNodeComparer()); 
        collection.Clear(); 
        collection.AddRange(nodes); 
    } 
 
    class TreeNodeComparer : IComparer 
    { 
        #region IComparer Members 
 
        public int Compare(object x, object y) 
        { 
            RadTreeNode firstNode = (RadTreeNode)x; 
            RadTreeNode secondNode = (RadTreeNode)y; 
 
            return firstNode.Text.CompareTo(secondNode.Text); 
        } 
        #endregion 
    } 

In the TreeNodeComparer class you can define what to be compared while sorting.

Attached, please find a small and running project on the matter.

Sincerely yours,
Nick
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
0
Ramachandra Ekbote
Top achievements
Rank 1
answered on 12 May 2009, 02:58 AM
Hi,

This code sorts the nodes on the server side. How can I do the same thing onb the client side?
please help.


regards
Ram.
Tags
TreeView
Asked by
Terry Webster
Top achievements
Rank 2
Answers by
Nikolay
Telerik team
Terry Webster
Top achievements
Rank 2
Ramachandra Ekbote
Top achievements
Rank 1
Share this question
or