|
Article relates to
|
RadTreeView v6.x, RadTreeView ASP.NET Ajax
|
|
Created by
|
Nick, Telerik
|
|
Last modified by
|
Yana, Telerik
|
Sorting TreeNodes
RadTreeView's Nodes can easily be sorted by Text/Value using simple server-side code:

[C#] Example
| protected void Page_Load(object sender, EventArgs e) |
| { |
| if (!Page.IsPostBack) |
| { |
| SortNodes(RadTreeView1.Nodes); |
| } |
| } |
| |
| //SortNodes is a recursive method enumerating and sorting all node levels |
| private void SortNodes(RadTreeNodeCollection collection) |
| { |
| Sort(collection); |
| foreach (RadTreeNode node in collection) |
| { |
| if (node.Nodes.Count > 0) |
| { |
| SortNodes(node.Nodes); |
| } |
| } |
| } |
| |
| //The Sort method is called for each node level sorting the child 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); |
| } |
| |
| //The TreeNodeComparer class defines the sorting criteria |
| 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 |
| } |
[VB] Example
| Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) |
| If Not Page.IsPostBack Then |
| SortNodes(RadTreeView1.Nodes) |
| End If |
| End Sub |
| |
| 'SortNodes is a recursive method enumerating and sorting all node levels |
| Private Sub SortNodes(ByVal collection As RadTreeNodeCollection) |
| Sort(collection) |
| For Each node As RadTreeNode In collection |
| If node.Nodes.Count > 0 Then |
| SortNodes(node.Nodes) |
| End If |
| Next |
| End Sub |
| |
| 'The Sort method is called for each node level sorting the child nodes |
| Public Sub Sort(ByVal collection As RadTreeNodeCollection) |
| Dim nodes As RadTreeNode() = New RadTreeNode(collection.Count - 1) {} |
| collection.CopyTo(nodes, 0) |
| Array.Sort(nodes, New TreeNodeComparer()) |
| collection.Clear() |
| collection.AddRange(nodes) |
| End Sub |
| |
| 'The TreeNodeComparer class defines the sorting criteria |
| Class TreeNodeComparer |
| Implements IComparer |
| #region IComparer Members |
| |
| Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements IComparer.Compare |
| Dim firstNode As RadTreeNode = DirectCast(x, RadTreeNode) |
| Dim secondNode As RadTreeNode = DirectCast(y, RadTreeNode) |
| |
| Return firstNode.Text.CompareTo(secondNode.Text) |
| End Function |
| #End Region |
| End Class |
| |

The RadTreeNodeCollection class does not have a CopyTo method. One could use the following method instead, to copy the Nodes from the collection to an Array:
[C#] Example
| //Copies the Nodes from the specified collection to the specified array. |
| private void Copy(RadTreeNodeCollection collection, RadTreeNode[] nodes) |
| { |
| for (int nodeIndex = 0; nodeIndex < collection.Count; nodeIndex++) |
| { |
| nodes[nodeIndex] = collection[nodeIndex]; |
| } |
| } |
[VB] Example
| 'Copies the Nodes from the specified collection to the specified array. |
| Private Sub Copy(ByVal collection As RadTreeNodeCollection, _ |
| ByVal nodes As RadTreeNode()) |
For nodeIndex As Integer = 0 To collection.Count - 1
|
| nodes(nodeIndex) = collection(nodeIndex) |
| Next |
| End Sub |
Please
Sign In
to rate this article.