Sveučilište u Zagrebu  |  Filozofski fakultet  |  Služba za informatiku  |  Kutak za korisnike  |  Teme

Collection Sorting Sample

' this sample demonstrates sorting abilities for System.Array and System.Collections.Generic.List(Of T) in VB.NET
'
' written by Gjuro Kladaric on 2015-10-26 for his own reference
'
' you can do with it whatever you want :-)
'
' data items are in the end of this module
'
' IComparer classes are after this module

Module Module1

    Enum IspisatiSto
        WorkingDataWithOriginals
        WorkingData
        Arrays
        Lists
    End Enum

    Sub Main()

        RefreshData()
        WriteDataOut("Originals:", IspisatiSto.WorkingDataWithOriginals)  ' write originals
        '===============================================================

        'sort by whole names
        Array.Sort(names)
        'this is identical to: Array.Sort(names, Function(name1 As String, name2 As String) String.Compare(name1, name2))
        Array.Sort(surnames)
        'this is identical to: Array.Sort(surnames, Function(name1 As String, name2 As String) String.Compare(name1, name2))

        WriteDataOut("After Array.Sort() on both arrays, independantly:", IspisatiSto.Arrays)  ' write originals
        '===============================================================
        RefreshData()

        ' sort by names from the second letter
        Array.Sort(names, Function(name1 As String, name2 As String) String.Compare(name1.Substring(1), name2.Substring(1)))


        WriteDataOut("After Array.Sort() on a substring from a second letter, with lambda function:", IspisatiSto.Arrays)
        '===============================================================
        RefreshData()

        Array.Sort(names, surnames)

        WriteDataOut("After Array.Sort() of two arrays together:", IspisatiSto.Arrays)
        '===============================================================
        RefreshData()

        Array.Sort(names, surnames, New myReverseComparer)

        WriteDataOut("After Array.Sort() of two arrays together, with IComparer, in reverse order:", IspisatiSto.Arrays)
        '===============================================================
        RefreshData()

        namesList.Sort()
        surnamesList.Sort()

        WriteDataOut("After List.Sort() of two lists independantly:", IspisatiSto.Lists)
        '===============================================================
        RefreshData()

        namesList.Sort(Function(name1 As String, name2 As String) String.Compare(name1, name2))
        surnamesList.Sort(Function(name1 As String, name2 As String) String.Compare(name1.Substring(1), name2.Substring(1)))

        WriteDataOut("After List.Sort() of two lists independantly, with lambda functions, surnames on a second letter:", IspisatiSto.Lists)
        '===============================================================
        RefreshData()

        namesList.Sort(New myReverseComparerOfString)
        surnamesList.Sort(New myReverseComparerOfString)

        WriteDataOut("After List.Sort() of two lists independantly, with IComparer, in reverse order:", IspisatiSto.Lists)

        Console.ReadLine()

    End Sub

    Sub WriteDataOut(label As String, Optional sto As IspisatiSto = IspisatiSto.WorkingData)

        Console.WriteLine()
        Console.WriteLine("===================== " & label)

        If sto = IspisatiSto.WorkingDataWithOriginals Then
            Console.WriteLine("NamesArray: " & String.Join("; ", namesArray))
            Console.WriteLine("SurnamesArray: " & String.Join("; ", surnamesArray))
        End If

        If sto = IspisatiSto.WorkingDataWithOriginals Or
                sto = IspisatiSto.WorkingData Or
                sto = IspisatiSto.Arrays Then
            Console.WriteLine("Names: " & String.Join("; ", names))
            Console.WriteLine("Surnames: " & String.Join("; ", surnames))
        End If

        If sto = IspisatiSto.WorkingDataWithOriginals Or
                sto = IspisatiSto.WorkingData Or
                sto = IspisatiSto.Lists Then
            Console.WriteLine("NamesList: " & String.Join("; ", namesList))
            Console.WriteLine("SurnamesList: " & String.Join("; ", surnamesList))
        End If

    End Sub

    Sub RefreshData()

        ReDim names(namesArray.Length - 1)
        namesArray.CopyTo(names, 0)
        names(0) = "Gjuro"

        ReDim surnames(surnamesArray.Length - 1)
        surnamesArray.CopyTo(surnames, 0)
        surnames(0) = "Kladariż"

        namesList.Clear() : namesList.AddRange(names)
        surnamesList.Clear() : surnamesList.AddRange(surnames)

    End Sub

    Dim names(), surnames() As String
    Dim namesList, surnamesList As New List(Of String)

    ReadOnly namesArray() As String = {
        "John",
        "Maria",
        "Ana",
        "Trevor",
        "Ivan",
        "Luke",
        "Stephen",
        "Eva",
        "Josephine"
    }

    ReadOnly surnamesArray() As String = {
        "Black",
        "Brown",
        "Green",
        "White",
        "Grey",
        "Blue",
        "Reed",
        "Johnson",
        "Carter"
    }

End Module

Class myReverseComparer
    Implements IComparer

    Public Function Compare(x As Object, y As Object) As Integer Implements IComparer.Compare

        If x.GetType Is GetType(String) AndAlso y.GetType Is GetType(String) Then
            Return -String.Compare(x, y)
        Else
            Return Nothing
        End If

    End Function

End Class

Class myReverseComparerOfString
    Implements IComparer(Of String)

    Public Function Compare(x As String, y As String) As Integer Implements IComparer(Of String).Compare

        Return -String.Compare(x, y)

    End Function

End Class

 

komentare molim ovdje