IComparable - 调用不同的排序?

发布于 2024-09-14 21:09:09 字数 204 浏览 4 评论 0原文

我有一个用于处理事务的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

沐歌 2024-09-21 21:09:09

这是我从最近的项目中摘录的一个示例。就像魅力一样。只需记住使用适当的函数调用 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)

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.

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

And call them like this...

tempList.Sort(AddressOf Classes.Purchaser.CompareByGroup)

or

tempList.Sort(AddressOf Classes.Purchaser.CompareByName)
撧情箌佬 2024-09-21 21:09:09

或者,如果您使用的是 .Net 3.5 或更高版本,则可以使用 linq。

dim orderedlistofdtos = (from e in listofdtos order by e.whatever select e).Tolist

Or you can use linq if you are on .Net 3.5 or above.

dim orderedlistofdtos = (from e in listofdtos order by e.whatever select e).Tolist
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文