Customize sorting of Treeview
I have a treeview that need to be sorted according to the tag of every node and also according to the alpha beta.
for example:
- Node1 , tag=A , text= Apple
- Node2, tag=B , text= Baloon
- Node3, tag=A, text= Help
I want to sort it, that nodes with tag A will be firsts, and just then nodes with tag B. but, i want the no开发者_C百科des that contains tag A, to be sorted from A to Z.
(the order = Node1,Node3,Node2)
please help me , How can i do it?
thanks in advance!
Assuming you're talking about System.Windows.Forms.Treeview, you can use TreeViewNodeSorter and an implementation of IComparer to create a custom sorting strategy.
http://msdn.microsoft.com/en-us/library/system.windows.forms.treeview.treeviewnodesorter.aspx
thanks! I did it like that:
/// <summary>
/// Create a node sorter that implements the IComparer interface.
/// </summary>
public class NodeSorter : IComparer
{
// compare between two tree nodes
public int Compare(object thisObj, object otherObj)
{
TreeNode thisNode = thisObj as TreeNode;
TreeNode otherNode = otherObj as TreeNode;
// Compare the types of the tags, returning the difference.
if (thisNode.Tag is first_type&& otherNode.Tag is another_type)
return 1;
//alphabetically sorting
return thisNode.Text.CompareTo(otherNode.Text);
}
}
精彩评论