Namespace E Public Class MyData Private _v As Integer Private _s As String Public Sub New() Me.New(0, "") End Sub Public Sub New(ByVal v As Integer, ByVal s As String) _v = v _s = s End Sub Public Property V() As Integer Get Return _v End Get Set _v = value End Set End Property Public Property S() As String Get Return _s End Get Set _s = value End Set End Property Public Overloads Overrides Function ToString() As String Return ("(" & _v & "," & _s & ")") End Function End Class Public Class MainClass Private Shared Function MyDataComparerV(ByVal o1 As MyData, ByVal o2 As MyData) As Integer Return (o1.V - o2.V) End Function Private Shared Function MyDataComparerS(ByVal o1 As MyData, ByVal o2 As MyData) As Integer Return o1.S.CompareTo(o2.S) End Function Public Shared Sub Main(ByVal args As String()) Dim mdc As List(Of MyData) = New List(Of MyData)() mdc.Add(New MyData(2, "A")) mdc.Add(New MyData(1, "CCC")) mdc.Add(New MyData(3, "BB")) For i As Integer = 0 To mdc.Count-1 Console.WriteLine(mdc(i)) Next mdc.Sort(AddressOf MyDataComparerV) For i As Integer = 0 To mdc.Count-1 Console.WriteLine(mdc(i)) Next mdc.Sort(AddressOf MyDataComparerS) For i As Integer = 0 To mdc.Count-1 Console.WriteLine(mdc(i)) Next End Sub End Class End Namespace
--- Public Sub Main(ByVal args As String()) Dim mdc As List(Of MyData) = New List(Of MyData)() mdc.Add(New MyData(1, "C")) mdc.Add(New MyData(1, "A")) mdc.Add(New MyData(1, "D")) mdc.Add(New MyData(2, "B"))
Console.WriteLine("unsorted") For i As Integer = 0 To mdc.Count - 1 Console.WriteLine(mdc(i)) Next
Console.WriteLine("MyDataComparerV") mdc.Sort(AddressOf MyDataComparerV) For i As Integer = 0 To mdc.Count - 1 Console.WriteLine(mdc(i)) Next Console.WriteLine("MyDataComparerS") mdc.Sort(AddressOf MyDataComparerS) For i As Integer = 0 To mdc.Count - 1 Console.WriteLine(mdc(i)) Next Console.ReadLine() End Sub
Private Shared Function MyDataComparerVS(ByVal o1 As MyData, ByVal o2 As MyData) As Integer Dim res As Integer = o1.V - o2.V If res = 0 Then Return o1.S.CompareTo(o2.S) Else Return res End If End Function
Synes godt om
Ny brugerNybegynder
Din løsning...
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.