Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: Visual Basic
Expertise: Beginner
Oct 24, 2001



Building the Right Environment to Support AI, Machine Learning and Deep Learning

Switch a Combo Box Sort from Alpha to Number

Don't forget to turn off the .sorted property on the combo box before calling!
ResortCboFromAlphaToNum cboAbc

Public Sub ResortCboFromAlphaToNum(cc As ComboBox)
    'resorts given combobox from alpha sort (usual) 
to numeric sort
    'e.g.  1,11,2,22 >> 1,2,11,22
    Dim ListStrArr() As String
    Dim ItemDataLngArr() As Long
    Dim tmpLng As Long
    Dim tmpStr As String

    Dim i As Integer
    Dim j As Integer
    Dim ListCountInt As Integer

    Dim LargestValLng As Long
    Dim ArrayPosInt As Integer
    ListCountInt = cc.ListCount

    If ListCountInt = 0 Then Exit Sub

    ReDim ListStrArr(ListCountInt - 1)
    ReDim ItemDataLngArr(ListCountInt - 1)

    For i = 0 To ListCountInt - 1
        ListStrArr(i) = cc.List(i)
        ItemDataLngArr(i) = cc.ItemData(i)
    'note, sorting operation is MUCH faster against 
arrays than with combo object
    'uses exchange sort, not optimal, but adequate
    For i = 0 To ListCountInt - 1
        LargestValLng = 0
        For j = i To ListCountInt - 1
            If LargestValLng <= val(ListStrArr(j)) 
                LargestValLng = val(ListStrArr(j))
                tmpStr = ListStrArr(i)
                tmpLng = ItemDataLngArr(i)

                ListStrArr(i) = ListStrArr(j)
                ItemDataLngArr(i) = ItemDataLngArr(j)

                ListStrArr(j) = tmpStr
                ItemDataLngArr(j) = tmpLng
            End If
        Next 'j
    Next 'i

    For i = 0 To ListCountInt - 1
        cc.List(i) = ListStrArr(ListCountInt - 1 - i)
        cc.ItemData(i) = ItemDataLngArr
(ListCountInt - 1 - i)
End Sub
Patrick Marshall
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date