IComparable - 调用不同的排序?
我有一个用于处理事务的 DTO。为了确保它按正确的顺序处理,我使用 iComparable 并对 DTO 的 List(of T) 进行排序。效果很好。然而,我刚刚得到另一个要求,即客户希望以不同的顺序输出...有没有办法允许我对同一对象有两种不同的排序,或者我是否需要复制当前类,将输出保存为该类型的新列表并使用该对象的新方式进行排序?似乎是一种糟糕的方法,但找不到任何可以让我这样做的方法。
I have a DTO that I am using to process transactions. To ensure that it is processing in the correct order, I am using iComparable and sorting the List(of T) of the DTO. That works great. However I just got another requirement that the customer wants the output in a different order... is there a way to allow me to have two different sorts for the same object, or do I need to copy the current class, save the output as a new List of that type and sort using the new way for that object? Seems like an awful way to do it, but cannot find anything that allows me to do it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是我从最近的项目中摘录的一个示例。就像魅力一样。只需记住使用适当的函数调用 SORT 即可。这超出了 IComparable 接口的范围,因此您可能希望将其从类声明中删除。
并这样称呼他们...
或者
Here is an example I ripped from a recent project. Works like a charm. Just have to remember to call SORT with the appropriate function. This is outside the scope fo the IComparable interface, so you might want to drop that from your class declaration.
And call them like this...
or
或者,如果您使用的是 .Net 3.5 或更高版本,则可以使用 linq。
Or you can use linq if you are on .Net 3.5 or above.