Sorting a dictionary on Key or Item?

Discussion in 'AutoCAD' started by Mark Propst, Sep 10, 2003.

  1. Mark Propst

    Mark Propst Guest

    Hi
    I'm trying to write a class to wrap the scripting dictionary object

    I was trying to make an add method that could add items in a sorted fashion
    (or be able to sort after the fact)

    can't seem to find any tips on google, just wondering if anyone's tackled
    this before?

    part of the class so far:
    Option Explicit
    Private mDict As Scripting.Dictionary
    Private Sub Class_Initialize()
    Set mDict = New Scripting.Dictionary
    End Sub

    Private Sub Class_Terminate()
    Set mDict = Nothing
    End Sub

    a non-functioning pseudo code idea for a sorted add
    Public Sub Add(ByVal sKeyName As String, ByVal vItem As Variant, ByVal
    iSorted As Integer)
    Dim lngIndex As Long

    Select Case iSorted
    Case Is = 0
    'sort by key property
    'if dict is begun, put item in correct slot
    If mDict.Count > 0 Then
    For lngIndex = 1 To mDict.Count
    'here, I know the item doesn't have a key property but if it
    did, I'd do something like this
    If sKeyName < mDict.Item(lngIndex).key Then

    'here I know the dictionary doestn' have a before: modifier but it it did,
    i'd try something like:
    mDict.Add sKeyName, vItem, before: mDict.Item (lngIndex)
    exit for

    End If
    Next lngIndex
    Else
    'just starting
    mDict.Add sKeyName, Item 'add first item

    End If

    Case Is = 1
    'sort on item property
    'etc
    End Select

    End Sub

    if I can't sort them as I insert them I guess I can sort them after the fact
    by creating a temp dict thus:

    Keyarray = sourceDict.Keys
    SortedArray = SortArray(keyarray)

    For i = 0 To ubound(sortedarray) 'Iterate the array
    key = SortedArray(i)
    item= SourceDict.Item(key)
    SortedDict.Add key, item
    next

    with this scenario sorting on the item may be harder because I can get an
    item from a key but I don't see how to get a key from an item

    the immediate impetus for the idea was I wanted to make a list of blocknames
    and their respective counts
    so I was going to use a dictionary, use the blockname for key and count for
    item
    this works but either i have to sort the blocknames first then put in
    dictionary, or sort the dictionary (what I was trying above), or collect the
    information from the dictionary and sort it afterwards when I want to read
    back the list

    I just thought a sortable Add method would save a couple steps

    Any thoughts?
    Tia
    Mark
     
    Mark Propst, Sep 10, 2003
    #1
  2. Mark Propst

    Rune Wold Guest

    You don't think this is a bit rich for counting block references?

    I would use a dynamic array and sort it. This task is so small that
    preformance is not an issue.

    Of course, if you want to go all the way, read up on balanced binary
    trees... :)
     
    Rune Wold, Sep 10, 2003
    #2
  3. Mark Propst

    Mark Propst Guest

    well, maybe I'm just dense....
    actually, the 'maybe' is superfluous
    :)
    The only routines I've seen to count block refs do nothing more than count
    the direct insertions. If that's all I needed to do, then I guess I
    wouldn't need a collection.

    as I said this was just the initial impetus to try to sort a collection by
    key, there could be other uses imaginable.
    For my purposes i need to count blocks but also eventually i need to also
    find certain entities meeting search criteria and measure and count them as
    well, etc.

    so I see a use for this in my situation beyond just counting blocks.

    in my present case however, i want to know how many "x" blocks there are
    whether they are direct insertions, or occurances embedded in 'y' blocks,
    which may have multiple occurances in 'z' blocks, any of which may be
    'local' blocks or xrefs.
    further I wanted to do this in object dbx since I may have to step through
    hundreds of local blocks and multiple external drawings(xrefs) iterating
    owner blocks (mspace or blockdefs) looking for embedded blocks, it seemed i
    needed a persistant collection object to hold and increment a tally of
    blocks.
    I don't want to iterate over model space 100 times if I have 100 blocks to
    count, i want to iterate once and each block goes into it's cubby hole and
    gets counted.
    well, it may seem small to you but I've been working on this off and on for
    a couple years when I have time - which isn't often - and still don't have
    anything functional so i'm obviously a very slow learner.

    so I'm trying to understand what you mean by using a dynamic array and
    sorting.
    do you mean a two dimensional array? one dimension for the block name and
    the other for the count?

    in lisp i'd use an association list (blockname . count)
    or (blockname count)

    or do you mean an array of arrays of two members
    (array
    subarray
    subarray
    subarray
    )
    where each sub array is
    (array blockname count)
    ?

    off the top of my head it seems like the code logic to handle this using
    arrays and haveing to redim everytime would be much more complicated than
    being able to call
    IncrementCount(collection.item(blockname))
    or something like that
    it seems like linked lists or hash tables are something similar to
    association lists in lisp,maybe that's what you're calling balanced binary
    trees
    unfortunately my ignorance is only exceeded by my lack of knowlege!

    back to the help files...

    anyway thanks for the response.
    Mark

    by the way, fwiw, i was finally able to sort the dictionary on keys by
    creating a temp dict and then swapping.
    I could do the same for the items but it would be more complex swap
    algorithm

    I'd also like to try to put keys/items into a dictionary in order in the
    first place, (like the collection.add before: syntax) rather than sorting
    afterwards but that seemed like it would take more complex code, and sorting
    afterwards is almost trivial so I guess it's no big deal.
     
    Mark Propst, Sep 11, 2003
    #3
  4. Mark Propst

    Mark Propst Guest

    Wayne,
    Thanks as always for your invaluable insights
    I'll check those links when I get time.
    Have a splendiferous day! :)
    Mark

    Mark,

    I've never worked with blocks so I only have a cursory knowledge of them
    based on what I've read on here in passing. This may be why I don't
    understand *why* you need your list sorted - you seem to be wanting to
    increment a count for each block name but I can't see why the list of names
    needs to be in order (or was it the counts that you wanted in order??).

    I only wanted to end up with a list alphabetically listed in a final
    'printout' or result - so I could quickly locate whatever name I was looking
    for at the time

    for the sake of gathering information it's unimportant

    as you point out there could be many ways to alphabetize the resultant list
     
    Mark Propst, Sep 11, 2003
    #4
  5. Hi Mark,
    I did an ATP course for AUGI a while back that you may find interesting.
    The thrust of the course wasn't data structures, but a good deal of it
    revolved around the creation of a List data structure which at one point
    evolved into a SortedList structure. This SortedList structure sorts
    objects as they are inserted onto the list according to the sort rule(s)
    that you set up for the objects being stored. I uploaded a portion of the
    class and the accompanying VB files to CF with the title "SortedList Class".

    Like I said, the focus of the course wasn't on building a solid data
    structure, but along with the excellent links that Wayne provided I think
    that you'll have enough ideas about data structures to use them to your
    advantage. Let me know if you have any questions!
     
    Bobby C. Jones, Sep 11, 2003
    #5
  6. Mark Propst

    Mark Propst Guest

    Thanks Bobby,
    I'll check it out
    Mark
     
    Mark Propst, Sep 11, 2003
    #6
  7. Mark Propst

    Mark Propst Guest

    dude!



    Thank you again for those fantastic links!



    That Francesco is one articulate brainiac is he not!?!



    And I mean that in the best sense of the word!



    :)~



    Mark



    &nbsp;



    "wivory" &lt;&gt; wrote in message news:...
     
    Mark Propst, Sep 12, 2003
    #7
  8. Mark Propst

    Mark Propst Guest

    Bobby,
    Thank you for that great education
    Well written, clear and informative and nice example of implements
    I've read about it many times but have n't used
    nice to see it in 'real life!'
    there's a lot to digest there!

    may your way be littered with happiness
    :)
     
    Mark Propst, Sep 12, 2003
    #8
  9. Thanks Mark, right back at ya!

    I've uploaded the previous section of the course to CF titled "Interface
    Implementation". The first half of this document describes in detail a much
    more common use of interfaces and the mechanics behind creating and using
    them. Enjoy <g>
     
    Bobby C. Jones, Sep 12, 2003
    #9
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.