Sae*_*ani 5 c# sorting treeview winforms
我正在尝试根据文本属性对树视图的节点进行排序.问题是我的比较课并不关心数字.这是代码:
public class TreeNodeSorter : IComparer
{
public int Compare(object x, object y)
{
var tx = x as TreeNode;
var ty = y as TreeNode;
return string.Compare(tx.Text, ty.Text);
}
}
Run Code Online (Sandbox Code Playgroud)
这是结果:
第一个子节点(Debug ...)没问题,但我的问题是为什么在地球上"HBM\D10"在"HBM\D7"之前排序等等......
如果可移植性不是问题,您可以p/invoke StrCmpLogicalW().Windows shell使用此函数对其显示的文件名进行排序:
public class TreeNodeSorter : IComparer
{
[DllImport("shlwapi.dll", CharSet = CharSet.Unicode, ExactSpelling = true)]
static extern int StrCmpLogicalW(string x, string y);
public int Compare(object x, object y)
{
var tx = x as TreeNode;
var ty = y as TreeNode;
return StrCmpLogicalW(tx.Text, ty.Text);
}
}
Run Code Online (Sandbox Code Playgroud)
请阅读http://www.dotnetperls.com/alphanumeric-sorting。您可能需要删除其他所有内容才能使他们的解决方案发挥作用 - 因为它们按数字或字母顺序排序。
如果它是动态文件名,您最好使用正则表达式来匹配您想要排序的部分。