Compare items using two variables

If you need help with a project or need to know how to do something specific in VB.NET then please ask your questions in here.
Forum rules
Please LOCK your topics once you have found the solution to your question so we know you no longer require help with your query.
4 posts Page 1 of 1
Contributors
User avatar
SumCode
Dedicated Member
Dedicated Member
Posts: 57
Joined: Fri Aug 03, 2012 2:34 am

Compare items using two variables
SumCode
I have two arrays in My.Settings (allWords and popularity). allWords contains a lot of words and popularity contains the same amt. of values as allWords, but instead of words it's numbers (1, 2, 3, etc.). Then I have an array of letters (etaoinshrdlcumwfgypbvkjxqz). So using that knowledge I want to compare two words by seeing if one has a higher 'popularity', then put it at the top, and if it doesn't, I want to check if it's first letter is, at least, closer to the beginning of the array of letters than the second words first letter.

So I came up with this code:
Code: Select all
Public Class AlphericComparer
    Implements IComparer

    Public Function Compare(ByVal a As Object, ByVal b As Object) As Integer Implements System.Collections.IComparer.Compare
        Dim firstLetter As Char = CType(a, Char)
        Dim secondLetter As Char = CType(b, Char)
        Dim firstWord As String = CType(a, String)
        Dim secondWord As String = CType(b, String)
        Dim alphabet As ArrayList = ArrayList.Adapter("etaoinshrdlcumwfgypbvkjxqz".ToCharArray)

        With My.Settings
            If .popularity(.allWords.IndexOf(firstWord)) > .popularity(.allWords.IndexOf(secondWord)) Then
                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
            ElseIf .popularity(.allWords.IndexOf(firstWord)) < .popularity(.allWords.IndexOf(secondWord)) Then
                If alphabet.IndexOf(firstLetter) < alphabet.IndexOf(secondLetter) Then
                    Return 0
                ElseIf alphabet.IndexOf(firstLetter) > alphabet.IndexOf(secondLetter) Then
                    Return 1
                Else
                    Return 0
                End If
            Else
                Return 0
            End If
        End With

    End Function
End Class
Unfortunately for me, this will return, when I enter some certain letters:
tango
amigo
photo
patio
outgo
outdo
motto
micro
metro
mango
macro
macho
lotto
limbo
lento
lasso
piano
plato
pluto
polio
turbo
Now every word, except for tango, has a popularity of 1 with tango having a popularity of 2. So it's good that it's at the top. However, according to my alphabet array, 'turbo' should come right after 'tango'. Also, 'photo' isn't with the other p's despite it not having a higher popularity than 1.

Any ideas are appreciated. Thanks for reading ;)

Full Source:
HangMan.zip
You do not have the required permissions to view the files attached to this post.
User avatar
CodenStuff
Site Admin
Site Admin
Posts: 4389
Joined: Tue Aug 04, 2009 1:47 am

I'm sorry I'm not completely clear on what it is you're trying to do :?
Welcome to CodenStuff.com Learn Code, Love Code. Thank you for being a member of the community.
User avatar
SumCode
Dedicated Member
Dedicated Member
Posts: 57
Joined: Fri Aug 03, 2012 2:34 am

CodenStuff wrote:
I'm sorry I'm not completely clear on what it is you're trying to do :?
So if I had the the list of words
tango
amigo
photo
patio
outgo
outdo
motto
micro
metro
mango
macro
macho
lotto
limbo
lento
lasso
piano
plato
pluto
polio
turbo
And I used the the comparer the program should output:
tango
turbo
amigo
outgo
outdo
lotto
limbo
lento
lasso
motto
micro
metro
mango
macro
macho
photo
patio
piano
plato
pluto
polio
Because tango has a (kind of) tag attached to it, which is the popularity value, of 2 and everything else has a tag of 1. And then, instead of alphabetical order, I want to it sort values using the order of "etaoinshrdlcumwfgypbvkjxqz". Basically it sorts the highest value at the top and if the there is more than 1 item that has a similar value sort it using the order of "etaoinshrdlcumwfgypbvkjxqz".

Hard to explain exactly, but I hope that helps
User avatar
SumCode
Dedicated Member
Dedicated Member
Posts: 57
Joined: Fri Aug 03, 2012 2:34 am

Finally I made the working code:
Code: Select all
Public Class PopularityComparer
    Implements IComparer

    Dim pop As Specialized.StringCollection
    Dim aW As Specialized.StringCollection

    Public Sub New(ByVal allWords As Specialized.StringCollection, ByVal popularity As Specialized.StringCollection)
        pop = popularity
        aW = allWords
    End Sub

    Public Function Compare(ByVal a As Object, ByVal b As Object) As Integer Implements System.Collections.IComparer.Compare
        Dim firstLetter As Char = CType(a, Char)
        Dim secondLetter As Char = CType(b, Char)
        Dim firstWord As String = CType(a, String)
        Dim secondWord As String = CType(b, String)
        Dim alphabet As ArrayList = ArrayList.Adapter("etaoinshrdlcumwfgypbvkjxqz".ToCharArray)

        If pop(aW.IndexOf(firstWord)) > pop(aW.IndexOf(secondWord)) Then
            Return -1
        ElseIf pop(aW.IndexOf(firstWord)) < pop(aW.IndexOf(secondWord)) 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
End Class
And when you run
tango,amigo,photo,patio,outgo,outdo,motto,micro,metro,mango,macro,macho,lotto,limbo,lento,lasso,piano,plato,pluto,polio,turbo
through it-it will output :
tango,turbo,amigo,outgo,outdo,lasso,lento,limbo,lotto,macho,macro,mango,metro,micro,motto,patio,piano,plato,pluto,polio,photo
Exactly how I wanted it to! :D
4 posts Page 1 of 1
Return to “Coding Help & Support”