How to sort a list of objects in by specific by size in VB.NET

1 Answer

0 votes
Class Test
    Implements IComparable(Of Test)

    Public Sub New(ByVal value As Integer)
        total = value
    End Sub

    Private total As Integer
    Public Property Size() As Integer
        Get
            Return total
        End Get
        Set(ByVal value As Integer)
            total = value
        End Set
    End Property

    Public Function CompareTo(different_object As Test) As Integer _
        Implements IComparable(Of Test).CompareTo
        Return Size().CompareTo(different_object.Size())
    End Function
End Class

Module Module1

    Sub Main()

        Dim list As List(Of Test) = New List(Of Test)

        list.Add(New Test(90))
        list.Add(New Test(20))
        list.Add(New Test(100))
        list.Add(New Test(80))
        list.Add(New Test(31))
        list.Add(New Test(30))

        list.Sort()

        For Each element As Test In list
            Console.WriteLine(element.Size())
        Next

    End Sub

End Module


' run:
' 
' 20
' 30
' 31
' 80
' 90
' 100

 



answered Oct 19, 2018 by avibootz
...