Sorting of Nodes after client add or drop

5 posts, 0 answers
  1. Terry Webster
    Terry Webster avatar
    48 posts
    Member since:
    Aug 2003

    Posted 21 Feb 2008 Link to this post

    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

  2. Nikolay
    Admin
    Nikolay avatar
    3997 posts

    Posted 22 Feb 2008 Link to this post

    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
  3. Terry Webster
    Terry Webster avatar
    48 posts
    Member since:
    Aug 2003

    Posted 25 Feb 2008 Link to this post

    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
  4. Nikolay
    Admin
    Nikolay avatar
    3997 posts

    Posted 26 Feb 2008 Link to this post

    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
  5. Ramachandra Ekbote
    Ramachandra Ekbote avatar
    3 posts
    Member since:
    Oct 2008

    Posted 11 May 2009 Link to this post

    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.
Back to Top