如何按多个值对列表框进行排序 [英] How do I sort a listbox by multiple values
本文介绍了如何按多个值对列表框进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嗨大家好
目前我有一个问题,试图对列表框进行排序,我遇到的问题是我试图按属性排序但没有任何运气
我需要对3种不同的属性进行排序
排序
Attribute1
然后
Attribute2
然后
Attribute3
但没有运气
我尝试过:
公共类CustomComparer:System.Collections.IComparer
{
public int Compare(object x,object y)
{
RadListBoxItem p1 = new RadListBoxItem();
RadListBoxItem p2 = new RadListBoxItem();
if(x是RadListBoxItem)
p1 = x as RadListBoxItem;
else
抛出新的ArgumentException(Object不是RadListBoxItem类型。);
if(y是RadListBoxItem)
p2 = y作为RadListBoxItem;
else
抛出新的ArgumentException(Object不是RadListBoxItem类型。);
int cmp = 0;
if(p1.ListBox.Sort == RadListBoxSort.Ascending)
{
//这里我们比较项目的值
if(String.Compare(p1.Value, p2.Value,!p1.ListBox.SortCaseSensitive)== 0)
{
return 0;
}
if(String.Compare(p1.Attributes [Attribute1],p2.Attributes [AgentHierachy],!p1.ListBox.SortCaseSensitive)== 0)
{
返回0;
}
if(String.Compare(p1.Attributes [Attribute2],p2.Attributes [SubAgentID],!p1.ListBox.SortCaseSensitive)== 0)
{
返回0;
}
}
if(p1.ListBox.Sort == RadListBoxSort.Descending)
{
//这里我们比较项目的值
cmp = String.Compare(p1.Value,p2.Value,!p1.ListBox.SortCaseSensitive)* -1;
}
返回cmp;
}
解决方案
如果这是WPF,你可以简单地使用绑定到控件的集合进行排序Linq:
collection.OrderBy(x => x.Property1).ThenBy(x => x.Property2)。 ThenBy(x => x.Property3)
当然,这只是一个概念性的代码,你必须适应你的代码。
Hi Guys
Currently i have an issue trying to sort a listbox, issue i am having is i have tried to sort by the attributes but not having any luck
I need to do the sort on 3 different attributes
sort by
Attribute1
then
Attribute2
then
Attribute3
but having no luck
What I have tried:
public class CustomComparer : System.Collections.IComparer { public int Compare(object x, object y) { RadListBoxItem p1 = new RadListBoxItem(); RadListBoxItem p2 = new RadListBoxItem(); if (x is RadListBoxItem) p1 = x as RadListBoxItem; else throw new ArgumentException("Object is not of type RadListBoxItem."); if (y is RadListBoxItem) p2 = y as RadListBoxItem; else throw new ArgumentException("Object is not of type RadListBoxItem."); int cmp = 0; if (p1.ListBox.Sort == RadListBoxSort.Ascending) { //here we compare the Values of the items if (String.Compare(p1.Value, p2.Value, !p1.ListBox.SortCaseSensitive) == 0) { return 0; } if (String.Compare(p1.Attributes["Attribute1"], p2.Attributes["AgentHierachy"], !p1.ListBox.SortCaseSensitive) == 0) { return 0; } if (String.Compare(p1.Attributes["Attribute2"], p2.Attributes["SubAgentID"], !p1.ListBox.SortCaseSensitive)==0) { return 0; } } if (p1.ListBox.Sort == RadListBoxSort.Descending) { //here we compare the Values of the items cmp = String.Compare(p1.Value, p2.Value, !p1.ListBox.SortCaseSensitive) * -1; } return cmp; }
解决方案
If this is WPF, you could simply sort the collection that's bound to the control using Linq:
collection.OrderBy(x=>x.Property1).ThenBy(x=>x.Property2).ThenBy(x=>x.Property3)
Of course, this is just conceptual code that you'll have to adapt to your code.
这篇关于如何按多个值对列表框进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文