Sort blocks alphabetically

Discussion in 'AutoCAD' started by Gilles, Jan 5, 2004.

  1. Gilles

    Gilles Guest

    Hi all,

    Does anyone know how to sort blocks alphabetically and not by order of
    creation.

    For example:
    ***
    For Each adblock In ThisDrawing.ModelSpace

    If Not TypeOf adblock Is AcadExternalReference And TypeOf adblock Is
    AcadBlockReference Then

    msgbox adblock.Name

    End If

    Next adblock
    ***
    I would like it to appear one after one But by alphabetical order...
    Thanks a lot in advance


    Gilles
     
    Gilles, Jan 5, 2004
    #1
  2. Add the names to an array or collection, then sort the array/collection
    ___________________________
    Mike Tuersley
    AutoCAD Clinic
    Rand IMAGINiT Technologies
     
    Mike Tuersley, Jan 5, 2004
    #2
  3. Gilles

    Gilles Guest

    Thanks Mike,

    I found one of your old message using a search in google.

    This is the code of the message:

    '========= Begin code chunk ======
    Public Sub BubbleSortCollection(colItems As Collection)
    '+--modified sub from Randall R. or Frank O.
    ' changed to work with collection instead of array
    Dim intCntr As Integer
    Dim intBubble As Integer
    Dim intTotal As Integer
    intTotal = colItems.Count
    Dim varList() As Variant
    ReDim varList(1 To intTotal)
    Dim varTemp As Variant
    '+--convert collection to array
    For intCntr = 1 To intTotal
    varList(intCntr) = colItems.Item(intCntr)
    Next
    '+--iterate thru array & sort it
    For intCntr = 1 To intTotal - 1
    For intBubble = intCntr + 1 To intTotal
    If varList(intCntr) > varList(intBubble) Then
    varTemp = varList(intCntr)
    varList(intCntr) = varList(intBubble)
    varList(intBubble) = varTemp
    End If
    Next
    Next
    '+--convert array back to collection
    Set colItems = New Collection
    For intCntr = 1 To intTotal
    colItems.Add varList(intCntr)
    Next
    End Sub
    '========= End code ======

    Seems to be very usefull but don't know how to use it in my case.
    How to integrate block objects in this kind of array or collection (this is
    my first array...)

    Thanks a lot in advance


    Gilles
     
    Gilles, Jan 6, 2004
    #3
  4. Gilles

    Mark Propst Guest

    Notice that his sub takes a collection as an argument.
    In your case, you want to populate a collection with the block names, then
    pass the collection to this sub.
    eg:
    Dim colBlockNames as New Collection
    Dim oBlk as AcadBlock
    Dim oBlocks as AcadBlocks
    Set oBlocks = ThisDrawing.Blocks

    For Each oBlk in oBlocks
    colBlockNames.Add oBlk.Name
    Next oBlk
    ....now your collection has all the names in it, but not sorted,
    ....pass the collectin to Mikes sub
    BubbleSortCollection colBlockNames

    .... now check the results
    Dim strBlockName as String
    For Each strBlockName in colBlockNames
    Debug.Print strBlockName
    Next strBlockName

    hth
    Mark
     
    Mark Propst, Jan 6, 2004
    #4
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.