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.
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
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:
Any ideas are appreciated. Thanks for reading ;)
Full Source:
So I came up with this code:
Code: Select all
Unfortunately for me, this will return, when I enter some certain letters: 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
tangoNow 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.
amigo
photo
patio
outgo
outdo
motto
micro
metro
mango
macro
macho
lotto
limbo
lento
lasso
piano
plato
pluto
polio
turbo
Any ideas are appreciated. Thanks for reading ;)
Full Source:
You do not have the required permissions to view the files attached to this post.
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.
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
tangoAnd I used the the comparer the program should output:
amigo
photo
patio
outgo
outdo
motto
micro
metro
mango
macro
macho
lotto
limbo
lento
lasso
piano
plato
pluto
polio
turbo
tangoBecause 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".
turbo
amigo
outgo
outdo
lotto
limbo
lento
lasso
motto
micro
metro
mango
macro
macho
photo
patio
piano
plato
pluto
polio
Hard to explain exactly, but I hope that helps
Finally I made the working code:
Code: Select all
And when you run 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
tango,amigo,photo,patio,outgo,outdo,motto,micro,metro,mango,macro,macho,lotto,limbo,lento,lasso,piano,plato,pluto,polio,turbothrough it-it will output :
tango,turbo,amigo,outgo,outdo,lasso,lento,limbo,lotto,macho,macro,mango,metro,micro,motto,patio,piano,plato,pluto,polio,photoExactly how I wanted it to!
4 posts
Page 1 of 1
Copyright Information
Copyright © Codenstuff.com 2020 - 2023