C# 自定义Treeview的排序

C# 自定义Treeview的排序,c#,winforms,sorting,treeview,icomparable,C#,Winforms,Sorting,Treeview,Icomparable,我有一个树状视图,需要根据每个节点的标签和alpha-beta进行排序 例如: Node1,tag=A,text=Apple Node2,标记=B,文本=Baloon Node3,tag=A,text=Help 我想对它进行排序,带有标记A的节点将是第一个,然后是带有标记B的节点。 但是,我希望包含标记A的节点从A到Z排序 (顺序=节点1、节点3、节点2) 请帮帮我, 我怎么做 提前谢谢 假设您讨论的是System.Windows.Forms.Treeview,您可以使用TreeViewNo

我有一个树状视图,需要根据每个节点的标签和alpha-beta进行排序

例如:

  • Node1,tag=A,text=Apple
  • Node2,标记=B,文本=Baloon
  • Node3,tag=A,text=Help
我想对它进行排序,带有标记A的节点将是第一个,然后是带有标记B的节点。 但是,我希望包含标记A的节点从A到Z排序

(顺序=节点1、节点3、节点2

请帮帮我, 我怎么做


提前谢谢

假设您讨论的是System.Windows.Forms.Treeview,您可以使用TreeViewNodeSorter和IComparer的实现来创建自定义排序策略


谢谢!我是这样做的:

 /// <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);
            }
        } 
//
///创建一个实现IComparer接口的节点分类器。
/// 
公共类节点所有者:IComparer
{
//比较两个树节点之间的差异
公共整数比较(对象thisObj、对象otherObj)
{
TreeNode thisNode=作为TreeNode的thisObj;
TreeNode otherNode=otherObj作为TreeNode;
//比较标记的类型,返回差异。
if(thisNode.Tag是第一种类型&&otherNode.Tag是另一种类型)
返回1;
//按字母顺序排序
返回thisNode.Text.CompareTo(otherNode.Text);
}
} 

只是澄清一下,您是否在顶部有一个标题,可以单击一次或两次并反转排序顺序ASC和DERSC?我不这么认为,如果我没记错的话,标准树视图没有标题。在这种情况下,不仅仅是定制排序,我们不应该说我们不会生成/填充具有特定顺序的树视图吗?