使用自己的 IComparer <T>使用 Linq OrderBy [英] Use own IComparer<T> with Linq OrderBy

查看:31
本文介绍了使用自己的 IComparer <T>使用 Linq OrderBy的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个通用的

List<MyClass>

其中 MyClass 有一个属性 InvoiceNumber,其中包含以下值:

where MyClass has a property InvoiceNumber which contains values such as:

200906/1
200906/2
..
200906/10
200906/11
200906/12

200906/1
200906/2
..
200906/10
200906/11
200906/12

我的列表绑定到一个

BindingList<T>

支持使用 linq 排序:

which supports sorting with linq:

protected override void ApplySortCore(
           PropertyDescriptor property, ListSortDirection direction)
{

    _sortProperty = property;
    _sortDirection = direction;

    var items = this.Items;

    switch (direction)
    {
        case ListSortDirection.Ascending:
            items = items.OrderByDescending(x => property.GetValue(x)).ToList();
            break;
        case ListSortDirection.Descending:
            items = items.OrderByDescending(x => property.GetValue(x)).ToList();
            break;
    }

    this.Items = items;

}

但是默认的比较器排序(按预期)是这样的:

However the default comparer sorts (as supposed) like this:

200906/1
200906/10
200906/11
200906/12
200906/2

200906/1
200906/10
200906/11
200906/12
200906/2

在这种情况下很糟糕.

现在我想使用我自己的IComparer.它看起来像这样:

Now I want to use my own IComparer<T> with this. It looks like this:

public class MyComparer : IComparer<Object>
{

    public int Compare(Object stringA, Object stringB)
    {
        String[] valueA = stringA.ToString().Split('/');
        String[] valueB = stringB.ToString().Split('/');

        if(valueA .Length != 2 || valueB .Length != 2)
             return String.Compare(stringA.ToString(), stringB.ToString());

        if (valueA[0] == valueB[0]) 
        {
          return String.Compare(valueA[1], valueB[1]);
        }
        else
        {
          return String.Compare(valueA[0], valueB[0]);
        }

    }

}

并更改了 ApplySortCore 代码以使用此 IComparer:

and changed the ApplySortCore code to use this IComparer:

case ListSortDirection.Ascending:
    MyComparer comparer = new MyComparer();
    items = items.OrderByDescending(
              x => property.GetValue(x), comparer).ToList();
    break;

当我调试代码时,我看到 MyComparer.Compare(object, object) 被多次调用并为比较方法返回正确的值 (-1, 0, 1).

When I debug my code, I see that MyComparer.Compare(object, object) is called multiple times and returns the right values (-1, 0, 1) for a compare method.

但我的列表仍然以错误"的方式排序.我错过了什么吗?我不知道.

But my list is still sorted the "wrong" way. Am I missing something? I have no clue.

推荐答案

我觉得你的比较器有问题.您仍然只是按照默认的文本顺序进行排序.您当然想解析这两个数字并基于此进行排序:

Your comparer looks wrong to me. You're still just sorting in the default text ordering. Surely you want to be parsing the two numbers and sorting based on that:

public int Compare(Object stringA, Object stringB)
{
    string[] valueA = stringA.ToString().Split('/');
    string[] valueB = stringB.ToString().Split('/');

    if (valueA.Length != 2 || valueB.Length != 2)
    {
        stringA.ToString().CompareTo(stringB.ToString());
    }

    // Note: do error checking and consider i18n issues too :)
    if (valueA[0] == valueB[0]) 
    {
        return int.Parse(valueA[1]).CompareTo(int.Parse(valueB[1]));
    }
    else
    {
        return int.Parse(valueA[0]).CompareTo(int.Parse(valueB[0]));
    }
}

(请注意,这不符合您的问题,即您已经调试并验证了 Compare 返回了正确的值 - 但恐怕我怀疑在这方面存在人为错误.)

(Note that this doesn't sit well with your question stating that you've debugged through and verified that Compare is returning the right value - but I'm afraid I suspect human error on that front.)

此外,Sven 的权利 - 更改 items 的值根本不会更改您的绑定列表.您应该添加:

Additionally, Sven's right - changing the value of items doesn't change your bound list at all. You should add:

this.Items = items;

在方法的底部.

这篇关于使用自己的 IComparer &lt;T&gt;使用 Linq OrderBy的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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