[英]Multisorting using VB.net 2.0
I'm stuck with VB.net 2.0 and I want to sort a List(Of MyObject)
. 我坚持使用VB.net 2.0,我想对List(Of MyObject)
进行排序。 I can easly do it using LINQ but as I'm back in the past with only using Framwork 2.0, I must say I don't realy know how to do this. 我可以使用LINQ轻松地做到这一点但是因为我过去只使用Framwork 2.0,我必须说我不知道如何做到这一点。 What is the best approach to do this? 这样做的最佳方法是什么?
I can have many SortFields as well as none... All this because user can choose fields he wants to sort by. 我可以有很多SortFields以及没有...这一切都是因为用户可以选择他想要排序的字段。
I search a lot about IComparer but it only do it for one field, not many... If I understand well how it works. 我搜索了很多关于IComparer但它只针对一个领域,而不是很多...如果我理解它是如何工作的。
What can I do? 我能做什么?
Look into creating a Comparer method that does the comparisons. 查看创建进行比较的Comparer方法。
For example, to create a comparer that uses two fields: (this is in C#, since my VB is somewhat rusty): 例如,要创建一个使用两个字段的比较器:(这是在C#中,因为我的VB有点生锈):
class MyCustomComparer: Comparer<MyObject>
{
public override int Compare(MyObject obj1, MyObject obj2)
{
int rslt = obj1.field1.CompareTo(obj2.field1);
if (rslt != 0)
{
rslt = obj1.field2.CompareTo(obj2.field2);
}
return rslt;
}
}
You can then create one of those objects and pass it to the List.Sort
overload that takes a comparison function. 然后,您可以创建其中一个对象并将其传递给采用比较函数的List.Sort
重载。
Things get a little more complicated when the user can specify the fields and the order to sort. 当用户可以指定字段和排序顺序时,事情会变得复杂一些。 What I've done in the pase is to create a method for each field that can be compared, for example: 我在pase中所做的是为每个可以比较的字段创建一个方法,例如:
private int CompareField1(MyObject obj1, MyObject obj2)
{
return obj1.field1.CompareTo(obj2.field1);
}
private int CompareField2(MyObject obj1, MyObject obj2)
{
return obj1.field2.CompareTo(obj2.field2);
}
And I create a list of function references in the constructor, one for each field that the user specifies. 我在构造函数中创建了一个函数引用列表,一个用户指定的每个字段。 It looks something like this: 它看起来像这样:
private List<Func<MyObject, MyObject, int>> compares;
public MyCustomComparer(List<int> fieldsToCompare)
{
compares = new List<Func<MyObject, MyObject, int>>();
for (int i = 0; i < fieldsToCompare.Count; ++i)
{
switch (fieldsToCompare[i])
{
case 1: compares.Add(CompareField1); break;
case 2: compares.Add(CompareField2); break;
// other fields here
}
}
}
Your CompareTo
method, then, loops through the compares
list: 然后,您的CompareTo
方法遍历compares
列表:
public override int Compare(MyObject obj1, MyObject obj2)
{
for (int i = 0; i < compares.Count; ++i)
{
int rslt = compares[i](obj1, obj2);
if (rslt != 0) return rslt;
}
return 0;
}
It's not especially pretty, but it's quite effective. 它不是特别漂亮,但它非常有效。
Even .NET 2.0 had List.Sort
. 甚至.NET 2.0都有List.Sort
。
In order to sort by more than one field of your data, adapt the comparer to sort by the fields consecutively: if the first comparison is equal, go by the second; 为了按数据的多个字段排序,调整比较器以按字段连续排序:如果第一个比较相等,则按第二个; if that is equal, go by the third; 如果相等,那就去第三个; etc. 等等
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.