将字符串日期转换为日期,以便在使用SortDescription时进行排序

4
我的WPF应用程序使用XMLDataProvider来提供数据,XML文件中有一个节点:
<RELEASEDATE>dd/mm/yyyy</RELEASEDATE>

对于列表中的每个项,我正在使用应用程序按照某种方式对数据进行排序。
Listbox1.Items.SortDescriptions.Add(new SortDescription("RELEASEDATE", ListSortDirection.Descending));

由于日期被视为字符串,因此结果与预期不符。

有什么更优雅的方法吗?我可以在行内将其转换为日期吗?

1个回答

2

您需要实现自己的IComparer:

class DateTimeComparer : IComparer
    {
        public int Compare(object x, object y)
        {
            //To Do : Implement DataTime Comparering
        }
    } 

现在将IComparer实现分配给集合的ListCollectionView.CustomSort:

 ListCollectionView view = new ListCollectionView(ListBox.Items);

 view.CustomSort = new DateTimeComparer();

See similar Question


谢谢,我会尽快实施! - Johan Verbelen

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接