トランザクションを処理するために使用している DTO があります。正しい順序で処理されていることを確認するために、iComparable を使用し、DTO の List(of T) を並べ替えています。それはうまくいきます。ただし、顧客が出力を別の順序で望んでいるという別の要件がありました...同じオブジェクトに対して2つの異なる並べ替えを許可する方法はありますか、または現在のクラスをコピーして出力を保存する必要がありますか?そのオブジェクトの新しい方法を使用して、そのタイプと並べ替えの新しいリスト?それを行うにはひどい方法のように思えますが、それを可能にするものを見つけることができません。
2 に答える
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)
于 2010-08-17T16:58:14.723 に答える
0
または、.Net 3.5 以降を使用している場合は、linq を使用できます。
dim orderedlistofdtos = (from e in listofdtos order by e.whatever select e).Tolist
于 2010-08-17T18:34:17.843 に答える