本文介绍了使用自己的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

我的目录绑定到

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

这是在这种情况下,讨厌

which is nasty in this case.

现在我想用我自己的的IComparer&LT; T&GT; 与此有关。它看起来是这样的:

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 code使用该的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;

在调试我的code,我看到 MyComparer.Compare(对象,对象)被多次调用,并返回正确的价值观(-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]));
    }
}

(请注意,这不符合您的问题,说明你已经通过调试和验证是比较返回正确的值坐好 - 但恐怕我在这一方面怀疑人为错误)

(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.)

此外,斯文的权利 - 改变项值不改变你的绑定列表。你应该加上:

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

this.Items = items;

在你的方法的底部。

at the bottom of your method.

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

10-19 12:01