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
Introduction:
To normally sort a list, one would use
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
-Step 3: Define what you will be comparing. For me, I am going to compare the first letters of words so I will use:
-Step 6: Use an 'ElseIf' statement to define what you want to stay at the bottom of an array (higher index). I used
-Step 7: If your first value = your second value it will put them right next to each other. So write 'Return 0'.
-Step 1: First you need to have an array so I just put random words in mine
My whole code looks like this
Anyways, thanks for reading.
To normally sort a list, one would use
Code: Select all
Unfortunately, this only sorts that array alphabetically. Fortunately, we can create our own way of sorting things, so let's jump into it!ArrayList.Adapter(exArray).Sort()
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
Then press enter and now you should have something like this: Implements IComparer
Code: Select all
The X and Y objects are what is going to be compared when running the code.Public Class AlphericComparer
Implements IComparer
Public Function Compare(x As Object, y As Object) As Integer Implements IComparer.Compare
End Function
End Class
-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
-Step 4 (Optional): Since I am going to be comparing words using a different alphabet, I will define it.Dim firstLetter As Char = CType(x, Char)
Dim secondLetter As Char = CType(y, Char)
Code: Select all
-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 isDim alphabet As ArrayList = ArrayList.Adapter("tashwiobmfcldpnegryuvjkqzx".ToCharArray)
Code: Select all
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) With alphabet
If .IndexOf(firstLetter) < .IndexOf(secondLetter) Then
Return -1
End If
End With
-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
Again, using 'Return 1', will put the first value at a higher index in your arrayElseIf .IndexOf(firstLetter) > .IndexOf(secondLetter) Then
Return 1
End If
-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
Part 2: Calling Your Comparer Else
Return 0
End If
-Step 1: First you need to have an array so I just put random words in mine
Code: Select all
-Step 2: Define your new classDim words As Array = {"abracadabra", "bonjour", "ciao", "auf", "hello", "zebra", "putin", "destroy"}
Code: Select all
-Step 3: Use the 'adapter' function of the 'ArrayList' class to sort your array using the new ComparerDim aComparer As New AlphericComparer
Code: Select all
You can also combine the previous step and this one using ArrayList.Adapter(words).Sort(aComparer)
Code: Select all
Part 3: ReflectionArrayList.Adapter(words).Sort(New AlphericComparer())
My whole code looks like this
Code: Select all
And when I call it, it looks like 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
Code: Select all
Before new comparer it will output
Dim aComparer As New AlphericComparer
Dim words As Array = {"abracadabra", "bonjour", "ciao", "auf", "hello", "zebra", "putin", "destroy"}
ArrayList.Adapter(words).Sort(New AlphericComparer())
abracadabraand after
auf
bonjour
ciao
destroy
hello
putin
zebra
abracadabraYou can also make a converter that goes by numbers
auf
hello
bonjour
ciao
destroy
putin
zebra
Code: Select all
Or if you like, by which string is longerPublic 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
Code: Select all
Or maybe, as I needed, using two variablesPublic 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
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
Copyright Information
Copyright © Codenstuff.com 2020 - 2023