Sort CheckedNodes (TreeNodeCollection) in ASP.NET Treeview

Generally it is required to sort all selected item in treeview and make it in CSV format. Suppose we require node value which is not same as text then we need to sort checked nodes (on the basis of text), not text only, So that we can retrieve node value and other node properties also.

The following code will sort TreeNodeCollection on text basis:

private TreeNodeCollection SortTreeNode(TreeNodeCollection nodeList)
{

for (int i = 0; i < nodeList.Count-1; i++)
{
for (int j = i + 1; j < nodeList.Count; j++)
{
if (nodeList[i].Text.CompareTo(nodeList[j].Text)>0)
{
TreeNode temp = nodeList[i];
nodeList.RemoveAt(i);
nodeList.AddAt(i,nodeList[j-1]);
nodeList.RemoveAt(j);
nodeList.AddAt(j, temp);
}

}
}
return nodeList;
}


No comments:

Post a Comment