列表框中的数字字符串排序 [英] Numerical string sorting in a listbox

查看:182
本文介绍了列表框中的数字字符串排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ListBox,其中ItemSource绑定到CollectionViewSource. CVS源是XmlDataProvider.因此,列表框列出了我指定的所有节点(名称属性). 现在这些节点具有属性,我希望ListBox由它们排序. 问题在于,由于基础数据是xml,所以每个值(节点的属性)都是字符串,但是某些值表示数值.由于使用CollectionViewSource.SortDescriptions.add (...)进行排序将按字母顺序对这些(字符串)值进行排序,因此将2,10,5的序列排序为10、2、5而不是2、5、10. 我该如何解决?

I have a ListBox which ItemSource is bound to a CollectionViewSource. The CVS Source is an XmlDataProvider. So the ListBox lists all nodes (name attribute) i specified. Now those nodes have attributes, and i want the ListBox to be sorted by them. The problem is, since the underlying data is xml, every value(attributes of the node) is a string, but some of the values represent numerical values. Since sorting with CollectionViewSource.SortDescriptions.add (...) will sort those (string)values alphabetically, a sequence of 2,10,5 will be sorted as 10,2,5 instead of 2,5,10. How can i solve this?

如果解决方案位于ListView的CustomSort中,请问有人可以为我提供有关如何使用基础XmlDocument进行此操作的快速示例吗?

If the solution lies in the ListView's CustomSort, could someone please provide me a quick example on how to to this with underlying XmlDocument?

我认为这和编写实现IComparer的类一样容易,但是我莫名其妙. 我想将属性的名称传递给方法,因此我可以从CVS中提取"所有这些属性,将它们转换为float(在这种情况下)并使用标准函数对其进行排序... 但是说实话,我对CustomSort的工作方式一无所知....

I thought it would be as easy as writing a class which implements IComparer, but somehow i am lost. I wanted to pass the name of the attribute to the method, so i could just "extract" all those attributes from the CVS, convert them to float (in this case) and sort them with standard functions... But i am totally lost on how this CustomSort works to be honest....

希望这是可能的,而无需放弃XmlDocument,因为它是给定的:)

Hope this is possible without ditching the XmlDocument, because it is kind of a given :)

致谢

推荐答案

如果绑定到从IList继承的集合,则可以从ListView控件的ItemsSource属性检索ListCollectionView.一旦有了ListCollectionView的实例,就可以将排序方法分配给CustomSorter属性.

If you are binding to a collection that inherits from IList, you can retrieve a ListCollectionView from the ItemsSource property of your ListView control. Once you have an instance of a ListCollectionView, you can assign a sorting method to the CustomSorter property.

自定义分类器必须继承旧的非通用IComparer样式.在Compare方法中,您将获得绑定类的两个实例.您可以根据需要投射它们,以获得所需的结果.在开发过程中,您可以将调试器锚定在Compare方法中,以确定确切的对象是什么.

The custom sorter must inherit from the old style, non-generic IComparer. Within the Compare method, you get two instances of the bound class. You can cast those as needed to get your desired result. During development, you can anchor the debugger inside the Compare method to determine exactly what the objects are.

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        List<string> strings = new List<string>() { "3", "2", "10", "1" };
        lv1.ItemsSource = strings;
        ListCollectionView lcv = 
             CollectionViewSource.GetDefaultView(lv1.ItemsSource) as ListCollectionView;
        if(lcv!=null)
        {
            lcv.CustomSort = new MySorter();
        }
    }
}
public class MySorter : IComparer
{
    public int Compare(object x, object y)
    { // set break point here!
        return Convert.ToInt32(x) - Convert.ToInt32(y);
    }
}

这篇关于列表框中的数字字符串排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆