使用自己的 IComparer使用 Linq OrderBy
我有一个泛型
List<MyClass>
,其中 MyClass
有一个属性 InvoiceNumber
,其中包含以下值:
200906/1
200906/2
..
200906/10
200906/11
200906/12
我的列表绑定到
BindingList<T>
支持使用 linq 排序的 a:
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;
}
但是默认比较器排序(如假设的)如下所示:
200906/1
200906/10
200906/11
200906/12
200906/2
在这种情况下这很糟糕。
现在我想使用我自己的 IComparer
。 它看起来像这样:
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
:
case ListSortDirection.Ascending:
MyComparer comparer = new MyComparer();
items = items.OrderByDescending(
x => property.GetValue(x), comparer).ToList();
break;
当我调试代码时,我看到 MyComparer.Compare(object, object)< /code> 被多次调用并为比较方法返回正确的值 (-1, 0, 1)。
但我的列表仍然以“错误”的方式排序。 我错过了什么吗? 我没有任何线索。
I have a generic
List<MyClass>
where MyClass
has a property InvoiceNumber
which contains values such as:
200906/1
200906/2
..
200906/10
200906/11
200906/12
My list is bound to a
BindingList<T>
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.
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]);
}
}
}
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;
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可以使用 Alphanum 算法:
AlphanumComparator
You can use The Alphanum Algorithm:
AlphanumComparator
排序列表仅绑定到局部变量 items,而不绑定到绑定列表的 Items 属性,因此它保持未排序状态。
[编辑] 基本上,您只是丢弃了排序工作的结果;-)
The sorted list is only bound to the local variable items, not to the Items property of your binding list, hence it remains unsorted.
[Edit] Basically, you're simply throwing away the result of your sorting efforts ;-)
有一个 Windows API那:
There is a Windows API for that:
你的比较器在我看来是错误的。 您仍然只是按照默认的文本顺序进行排序。 当然,您希望解析这两个数字并基于此进行排序:(
请注意,这与您的问题不太相符,表明您已经调试并验证了 Compare 返回正确的值 - 但恐怕我怀疑这方面的人为错误。)
此外,Sven 是对的 - 更改
items
的值根本不会更改您的绑定列表。 添加: 。您应该在方法的底部
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:
(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:at the bottom of your method.
我遇到了一般自然排序的问题,并在此处发布了解决方案:
自然排序与 Linq OrderBy() 比较
I encountered the issue of general natural sorting and blogged the solution here:
Natural Sort Compare with Linq OrderBy()
我们不能这样做吗:
用法:
这与:
Can't we do like this:
Usage:
This is same as :