我有一个DTO,我正在使用它来处理事务。为了确保它按正确的顺序处理,我使用iComparable并对DTO的List(T)进行排序。这很好。然而,我刚刚得到另一个要求,即客户希望以不同的顺序输出......有没有办法让我对同一个对象有两种不同的排序,或者我需要复制当前类,将输出保存为一个新类型的列表并使用该对象的新方法进行排序?看起来像一个可怕的方式来做到这一点,但找不到任何事情让我做到这一点。IComparable - 调用不同的类别?
0
A
回答
2
这是我从最近的一个项目中撷取的例子。奇迹般有效。只需要记住用适当的函数调用SORT。这超出了IComparable接口的范围,所以您可能希望从类声明中删除它。
Public Class Purchaser
....
Public Shared Function CompareByGroup(_
ByVal x As Purchaser, ByVal y As Purchaser) As Integer
If x Is Nothing Then
If y Is Nothing Then
' If x is Nothing and y is Nothing, they're equal.
Return 0
Else
' If x is Nothing and y is not Nothing, y is greater.
Return -1
End If
Else
If y Is Nothing Then
' If x is not Nothing and y is Nothing, x is greater.
Return 1
Else
' ...and y is not Nothing, compare by GroupName.
Return x.GroupName.CompareTo(y.GroupName)
End If
End If
End Function
Public Shared Function CompareByName(_
ByVal x As Purchaser, ByVal y As Purchaser) As Integer
... 'you get the idea
End Function
,并呼吁他们这样......
tempList.Sort(AddressOf Classes.Purchaser.CompareByGroup)
或
tempList.Sort(AddressOf Classes.Purchaser.CompareByName)
0
或者如果你是对的.Net 3.5或以上,你可以使用LINQ。
dim orderedlistofdtos = (from e in listofdtos order by e.whatever select e).Tolist
相关问题
- 1. 不同类别的调用方法
- 2. 为什么STL算法针对不同类别调用不同?
- 3. 不同类别
- 4. 不同类别
- 5. IComparable与IComparable之间的区别<K>
- 6. FxCop和IComparable/IComparable <T>
- 7. 使用随机来调用不同类别的字典
- 8. 如何使用BigCartel“变量”调用不同的产品类别
- 9. 来自不同类别的对象的调用方法
- 10. 来自不同类别的调用方法
- 11. 来自不同类别swift的调用var
- 12. Java:来自不同类别的调用函数
- 13. 如何调用不同类别的功能
- 14. 从非活动类别的不同活动中调用方法
- 15. Winforms:调用来自不同类别的输入表单功能
- 16. 不同类别之间的函数调用
- 17. 通过不同类别的Arraylist调用和搜索
- 18. SQL - 不同类别的列
- 19. 不同类别的GCD
- 20. 类别和子类别在相同或不同的表?
- 21. 重用Junit的不同类别
- 22. 使用不同类别的代理
- 23. 不同类别的不同日志级别
- 24. 使用IComparable
- 25. .net值类使用IComparable排序
- 26. IComparable和IEquatable接口有什么区别?
- 27. Lucene.Net - 获取不同类别
- 28. Array.BinarySearch找不到使用IComparable的项目
- 29. 在不同类别中使用Arraylist
- 30. 调用不同类的继承层次
完美的感谢,我有我的比较例程,但不能为我的生活得到正确的语法。 – RiddlerDev 2010-08-18 13:46:40