Results 1 to 9 of 9

Thread: bubble sorting a dictionary object ?

  1. #1

    Thread Starter
    Member
    Join Date
    Aug 2001
    Posts
    37

    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:
    1. dict = bubble_sort(dict.items)
    2.  
    3. Private Function bubble_sort(dict)
    4. Dim i As Long, n As Long, tempN As Long, allOk As Boolean
    5. For n = 0 To UBound(dict)
    6.    allOk = True
    7.    For i = 0 To UBound(dict) - 1
    8.    If dict(i) > dict(i + 1) Then
    9.        allOk = False
    10.        tempN = dict(i)
    11.        dict(i) = dict(i + 1)
    12.        dict(i + 1) = tempN
    13.    End If    Next
    14.    If allOk Then Exit For
    15. Next
    16. End Function

    What do I appear to be doing incorrectly?
    Last edited by dani61265; May 15th, 2002 at 11:14 AM.

  2. #2
    Fanatic Member
    Join Date
    Jul 2001
    Location
    London UK
    Posts
    671
    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)

  3. #3

    Thread Starter
    Member
    Join Date
    Aug 2001
    Posts
    37
    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?

  4. #4

    Thread Starter
    Member
    Join Date
    Aug 2001
    Posts
    37
    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?

  5. #5

    Thread Starter
    Member
    Join Date
    Aug 2001
    Posts
    37
    .

  6. #6
    Lively Member Wally Pipp's Avatar
    Join Date
    Jan 2002
    Location
    Carnivàle
    Posts
    79
    try to return a dict obj :

    VB Code:
    1. Dim dict as variant ()
    2.  
    3. dict = bubble_sort(dict.items)
    4.  
    5. Private Function bubble_sort(dict2 as Variant()) As Variant ()
    6. Dim i As Long, n As Long, tempN As Long, allOk As Boolean
    7. For n = 0 To UBound(dict2)
    8.    allOk = True
    9.    For i = 0 To UBound(dict2) - 1
    10.    If dict2(i) > dict2(i + 1) Then
    11.        allOk = False
    12.        tempN = dict2(i)
    13.        dict2(i) = dict2(i + 1)
    14.        dict2(i + 1) = tempN
    15.    End If    Next
    16.    If allOk Then Exit For
    17. Next
    18. bubble_sort = dict2
    19. 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"

  7. #7

    Thread Starter
    Member
    Join Date
    Aug 2001
    Posts
    37
    Still not functioning correctly. It is only returning the sorted occurences. It does not sort the occurences with thier respective word.

  8. #8
    Lively Member Wally Pipp's Avatar
    Join Date
    Jan 2002
    Location
    Carnivàle
    Posts
    79
    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"

  9. #9

    Thread Starter
    Member
    Join Date
    Aug 2001
    Posts
    37
    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
  •  



Click Here to Expand Forum to Full Width