How To Sort A List Using A Custom Method

Heres your chance to share your own tutorials with the community. Just post them on here. If your lucky they may even be posted on the main site.
1 post Page 1 of 1
Contributors
User avatar
SumCode
Dedicated Member
Dedicated Member
Posts: 57
Joined: Fri Aug 03, 2012 2:34 am

Introduction:
To normally sort a list, one would use
Code: Select all
ArrayList.Adapter(exArray).Sort()
Unfortunately, this only sorts that array alphabetically. Fortunately, we can create our own way of sorting things, so let's jump into it!

Part 1: Creating the Comparer Class
-Step 1: Create the new class by right clicking your project>Add>Class (name whatever you want).
-Step 2: In your new class, type
Code: Select all
Implements IComparer
Then press enter and now you should have something like this:
Code: Select all
Public Class AlphericComparer
    Implements IComparer

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

    End Function
End Class
The X and Y objects are what is going to be compared when running the code.
-Step 3: Define what you will be comparing. For me, I am going to compare the first letters of words so I will use:
Code: Select all
Dim firstLetter As Char = CType(x, Char)
        Dim secondLetter As Char = CType(y, Char)
-Step 4 (Optional): Since I am going to be comparing words using a different alphabet, I will define it.
Code: Select all
Dim alphabet As ArrayList = ArrayList.Adapter("tashwiobmfcldpnegryuvjkqzx".ToCharArray)
-Step 5: For me, I want whichever letter has a lower index in the newly-defined alphabet goes at the top of the list. What I write for that is
Code: Select all
 With alphabet
            If .IndexOf(firstLetter) < .IndexOf(secondLetter) Then
                Return -1
            End If
        End With
When one writes 'Return -1', this means you want whatever your first value is, you want it to stay at the top of your array (a.k.a. keep a lower index)
-Step 6: Use an 'ElseIf' statement to define what you want to stay at the bottom of an array (higher index). I used
Code: Select all
ElseIf .IndexOf(firstLetter) > .IndexOf(secondLetter) Then
                Return 1
            End If
Again, using 'Return 1', will put the first value at a higher index in your array
-Step 7: If your first value = your second value it will put them right next to each other. So write 'Return 0'.
Code: Select all
 Else
                Return 0
            End If
Part 2: Calling Your Comparer
-Step 1: First you need to have an array so I just put random words in mine
Code: Select all
Dim words As Array = {"abracadabra", "bonjour", "ciao", "auf", "hello", "zebra", "putin", "destroy"}
-Step 2: Define your new class
Code: Select all
Dim aComparer As New AlphericComparer
-Step 3: Use the 'adapter' function of the 'ArrayList' class to sort your array using the new Comparer
Code: Select all
ArrayList.Adapter(words).Sort(aComparer)
You can also combine the previous step and this one using
Code: Select all
ArrayList.Adapter(words).Sort(New AlphericComparer())
Part 3: Reflection
My whole code looks like this
Code: Select all
Public Class AlphericComparer
    Implements IComparer

    Public Function Compare(x As Object, y As Object) As Integer Implements IComparer.Compare
        Dim firstLetter As Char = CType(x, Char)
        Dim secondLetter As Char = CType(y, Char)
        Dim alphabet As ArrayList = ArrayList.Adapter("tashwiobmfcldpnegryuvjkqzx".ToCharArray)

        With alphabet
            If .IndexOf(firstLetter) < .IndexOf(secondLetter) Then
                Return -1
            ElseIf .IndexOf(firstLetter) > .IndexOf(secondLetter) Then
                Return 1
            Else
                Return 0
            End If
        End With
    End Function
End Class
And when I call it, it looks like
Code: Select all
  Dim aComparer As New AlphericComparer
        Dim words As Array = {"abracadabra", "bonjour", "ciao", "auf", "hello", "zebra", "putin", "destroy"}
        ArrayList.Adapter(words).Sort(New AlphericComparer())
Before new comparer it will output
abracadabra
auf
bonjour
ciao
destroy
hello
putin
zebra
and after
abracadabra
auf
hello
bonjour
ciao
destroy
putin
zebra
You can also make a converter that goes by numbers
Code: Select all
Public Function Compare(x As Object, y As Object) As Integer Implements IComparer.Compare
        Dim firstNum As Integer = CType(x, Integer)
        Dim secondNum As Integer = CType(y, Integer)

        If firstNum > secondNum Then
            Return -1
        ElseIf firstNum < secondNum Then
            Return 1
        Else
            Return 0
        End If
    End Function
Or if you like, by which string is longer
Code: Select all
Public Function Compare(x As Object, y As Object) As Integer Implements IComparer.Compare
        Dim firstWord As String = CType(x, String)
        Dim secondWord As String = CType(y, String)

        If firstWord.Length > secondWord.Length Then
            Return -1
        ElseIf firstWord.Length < secondWord.Length Then
            Return 1
        Else
            Return 0
        End If
    End Function
Or maybe, as I needed, using two variables
Code: Select all
    Public Function Compare(x As Object, y As Object) As Integer Implements IComparer.Compare
        Dim firstLetter As Char = CType(x, Char)
        Dim secondLetter As Char = CType(y, Char)
        Dim firstWord As String = CType(x, String)
        Dim secondWord As String = CType(y, String)
        Dim alphabet As ArrayList = ArrayList.Adapter("abcdefghijklmnoprstuvwxyz".ToCharArray)

        If firstWord.Length > secondWord.Length Then
            Return -1
        ElseIf firstWord.Length < secondWord.Length Then
            Return 1
        Else
            If alphabet.IndexOf(firstLetter) < alphabet.IndexOf(secondLetter) Then
                Return -1
            ElseIf alphabet.IndexOf(firstLetter) > alphabet.IndexOf(secondLetter) Then
                Return 1
            Else
                Return 0
            End If
        End If
    End Function

Anyways, thanks for reading. :)
1 post Page 1 of 1
Return to “Tutorials”