Listbox - sorting alphabetically?

Discussion in 'AutoCAD' started by Pienpeas, Jun 24, 2004.

  1. Pienpeas

    Pienpeas Guest

    Hi,

    Simple question, I've populated a listbox with layer names but they are in no certain order. I thought that the listbox has a sorted property but it wont let me do

    Listbox1.sorted=true

    Any help appreciated

    Al
     
    Pienpeas, Jun 24, 2004
    #1
  2. Pienpeas

    TomD Guest

    I'm not aware of any sort property for listboxes. I think you'd have to
    sort them first. I haven't done it, but if you have them in an array, I've
    seen a few sorting functions on the net, then just pass it to the listbox
    with the List property.

    HTH

    no certain order. I thought that the listbox has a sorted property but it
    wont let me do
     
    TomD, Jun 24, 2004
    #2
  3. Pienpeas

    Norman Yuan Guest

    VBA ListBox in ACAD does not do sorting for you. You have do it yourself:
    put all layers' name in an string array; choose one of your favorite sorting
    algrithm and write your sorting code; sort the array, and then popualte the
    listbox/combobox.

    Actaully, you do not have to write your onw sorting code, there are a lot
    ready to use sorting algrithms on the net. You can google some VB/VBA
    related news group for "sorting". Here is one example of what I have found:

    Public Sub ShellSort(ByRef A() As Variant, _
    ByVal Lb As Long, ByVal Ub As Long)
    Dim n As Long, h As Long
    Dim i As Long, j As Long
    Dim t As Variant
    '
    ' ***** If you are using this routine to sort string data and you want
    ' it to be case insensitive (ie "cat" comes before "Dog") then you
    ' must place the line "Option Compare Text" in the (general)
    ' (declarations) section of the Form or Module in which this routine
    ' is declared
    ' *******************************
    '
    ' We allow the user to send his own start and end positions to
    ' this routine in case he wants to sort only part of the array
    ' (which is a common requirement) so we must first check and
    ' adjust those parameters in case he has given us silly numbers!
    If Ub > UBound(A) Then Ub = UBound(A)
    If Lb < LBound(A) Then Lb = LBound(A)
    If Lb >= Ub Then Exit Sub
    '
    ' now sort array (from element lb to element ub)
    ' first calculate the largest increment
    n = Ub - Lb + 1
    h = 1
    If n > 13 Then
    Do While h < n
    h = 3 * h + 1
    Loop
    h = h \ 3
    If h > (n * 0.8) Then h = h \ 3
    End If
    ' repeatedly do an insertion sort using
    ' values of h from its initial value down
    ' to a value of 1
    Do While h > 0
    For i = Lb + h To Ub
    t = A(i)
    For j = i - h To Lb Step -h
    If A(j) <= t Then Exit For
    A(j + h) = A(j)
    Next j
    A(j + h) = t
    Next i
    h = h \ 3
    Loop
    End Sub


    no certain order. I thought that the listbox has a sorted property but it
    wont let me do
     
    Norman Yuan, Jun 24, 2004
    #3
  4. Pienpeas

    AKS Guest

    Here is what I use for what it is worth. Here cbLayers is a combobox
    in ufKeyInc. All the xref layers are excluded in this use.


    Function FillLayerCBox() As Integer
    Dim f As Integer
    Dim ActLayArry() As Variant
    Dim x As Integer
    Dim count As Integer
    Dim layer As AcadLayer
    ufKeyInc.cbLayers.Clear
    For Each layer In ThisDrawing.layers
    f = InStr(1, UCase(layer.Name), UCase("|"))
    If f = 0 Then
    count = count + 1
    ReDim Preserve ActLayArry(0 To count - 1)
    ActLayArry(UBound(ActLayArry)) = UCase(layer.Name)
    End If
    Next
    QSort ActLayArry
    ufKeyInc.cbLayers.List = ActLayArry
    ufKeyInc.cbLayers.ListRows = ufKeyInc.cbLayers.ListCount
    On Error GoTo 0
    End Function

    Private Sub QSort(List() As Variant) 'an extremely fast sort method!!
    ' Sorts an array using Quick Sort algorithm
    ' Adapted from "Visual Basic Developers Guide"
    Dim i, j, B, k As Integer
    Dim l As Integer, t As String, r As Integer, d As Integer
    Dim p(1 To 100) As Integer
    Dim w(1 To 100) As Integer
    On Error GoTo ErrExit
    k = 1
    p(k) = LBound(List)
    w(k) = UBound(List)
    l = 0 ' was 1
    d = 1
    r = UBound(List)
    Do
    toploop:
    If r - l < 9 Then GoTo bubsort
    i = l
    j = r
    While j > i
    If UCase(List(i)) > UCase(List(j)) Then
    t = List(j)
    List(j) = List(i)
    List(i) = t
    d = -d
    End If
    If d = -1 Then
    j = j - 1
    Else
    i = i + 1
    End If
    Wend
    j = j + 1
    k = k + 1
    If i - l < r - j Then
    p(k) = j
    w(k) = r
    r = i
    Else
    p(k) = l
    w(k) = i
    l = j
    End If
    d = -d
    GoTo toploop
    bubsort:
    If r - l > 0 Then
    For i = l To r
    B = i
    For j = B + 1 To r
    If UCase(List(j)) <= UCase(List(B)) Then B = j
    Next j
    If i <> B Then
    t = List(B)
    List(B) = List(i)
    List(i) = t
    End If
    Next i
    End If
    l = p(k)
    r = w(k)
    k = k - 1
    Loop Until k = 0
    ErrExit:
    On Error GoTo 0
    End Sub
     
    AKS, Jun 24, 2004
    #4
  5. The VB version of the listbox has a Sort property.
    The VBA version does not.

    no certain order. I thought that the listbox has a sorted property but it
    wont let me do
     
    Mark Johnston, Jun 25, 2004
    #5
  6. Pienpeas

    wivory Guest

    Most listboxes don't have very many entries in them. For lists of under 1000 items a Bubble Sort is recommended.

    Regards

    Wayne Ivory
    IT Analyst Programmer
    Wespine Industries Pty Ltd
     
    wivory, Jun 25, 2004
    #6
  7. Pienpeas

    pienpeas Guest

    Thanks guys, its obviously not as simple as i thought!
     
    pienpeas, Jun 25, 2004
    #7
  8. Pienpeas

    Miles Guest

    I ended up using a combobox for my list of layers, too much hassle w/a
    listbox.

    Dim intTeller As Integer
    Dim intRuns As Integer
    Dim varTemp As Variant

    Dim entry As AcadLayer

    Dim arrLayers() As Variant
    Dim iCount As Integer

    For Each entry In ThisDrawing.Layers
    ReDim Preserve arrLayers(iCount)
    arrLayers(iCount) = entry.Name
    iCount = iCount + 1
    Next

    For intRuns = 0 To UBound(arrLayers()) - 1
    For intTeller = 0 To UBound(arrLayers()) - 1
    If arrLayers(intTeller) > arrLayers(intTeller + 1) Then
    varTemp = arrLayers(intTeller + 1)
    arrLayers(intTeller + 1) = arrLayers(intTeller)
    arrLayers(intTeller) = varTemp
    End If
    Next intTeller
    Next intRuns

    For intRuns = 0 To UBound(arrLayers())
    cmbLayers.AddItem arrLayers(intRuns)
    Next intRuns

    no certain order. I thought that the listbox has a sorted property but it
    wont let me do
     
    Miles, Jun 30, 2004
    #8
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.