|
-
May 15th, 2002, 10:40 AM
#1
Thread Starter
Member
bubble sorting a dictionary object ?
I have a dictionary consisting of an array of words as the keys and the number of occurences of each word as the items. I want sort this dictionary by number of occurences of each key, in descending order, so that I may be able to take the top 20 most used words. I want to use a bubble sort but I seem to be having trouble passing my dictionary to the sort function. Here is some of my code...
VB Code:
dict = bubble_sort(dict.items)
Private Function bubble_sort(dict)
Dim i As Long, n As Long, tempN As Long, allOk As Boolean
For n = 0 To UBound(dict)
allOk = True
For i = 0 To UBound(dict) - 1
If dict(i) > dict(i + 1) Then
allOk = False
tempN = dict(i)
dict(i) = dict(i + 1)
dict(i + 1) = tempN
End If Next
If allOk Then Exit For
Next
End Function
What do I appear to be doing incorrectly?
Last edited by dani61265; May 15th, 2002 at 11:14 AM.
-
May 15th, 2002, 11:01 AM
#2
Fanatic Member
1. To Format your posts use [vbcode*] [/vbcode*] tags around the whole of the code (without the *s)
2 Change your function back to
Private Function bubble_sort(arrInt) or replace all mentions of arrInt with dict within that procedure!
3. Pass it an array NOT an object - So in your case the first step would be to Bubble_Sort(Dict.Items)
-
May 15th, 2002, 12:26 PM
#3
Thread Starter
Member
OK. But it appears that I am losing the sort when I leave my sort function. I don't think I am passing my parameters (dictionary) correctly. Any ideas?
-
May 15th, 2002, 03:25 PM
#4
Thread Starter
Member
Is it even possible to sort\re-order a dictionary object like this? I have to have tried a hundred different things and as of yet unable to accomplish this?
-
May 16th, 2002, 08:04 AM
#5
Thread Starter
Member
-
May 16th, 2002, 08:14 AM
#6
Lively Member
try to return a dict obj :
VB Code:
Dim dict as variant ()
dict = bubble_sort(dict.items)
Private Function bubble_sort(dict2 as Variant()) As Variant ()
Dim i As Long, n As Long, tempN As Long, allOk As Boolean
For n = 0 To UBound(dict2)
allOk = True
For i = 0 To UBound(dict2) - 1
If dict2(i) > dict2(i + 1) Then
allOk = False
tempN = dict2(i)
dict2(i) = dict2(i + 1)
dict2(i + 1) = tempN
End If Next
If allOk Then Exit For
Next
bubble_sort = dict2
End Function
try it in that fashion
A post brought to you by the Grim Reaper Appreciation Society™
"Buy your lifetime subscription now and save on your coffin"
-
May 16th, 2002, 08:51 AM
#7
Thread Starter
Member
Still not functioning correctly. It is only returning the sorted occurences. It does not sort the occurences with thier respective word.
-
May 16th, 2002, 08:59 AM
#8
Lively Member
if your dict.items is only the occurences, the function will only sort the occurences then. Include the words in the array.
You could use a multidimensional as follows :
Dim MyArray(20,1) as variant
You fill the array with the occurances in the first and the corresponding word in the second.
Sort this array with the function.
A post brought to you by the Grim Reaper Appreciation Society™
"Buy your lifetime subscription now and save on your coffin"
-
May 16th, 2002, 11:34 AM
#9
Thread Starter
Member
Duhhh..Of course. Yes, you are right. I should have thought of that first thing. Thanks for the wake up call!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|