Solved

Combo Sort

Posted on 2000-04-18
7
529 Views
Last Modified: 2008-02-20
How can I sort NUMBERS in a combobox?
I'm using .sort method, but it sort the numbers as they were strings:
1
10
100
2
20
200
Thanks for your help.
Direct help would be appreciated rather then long workarounds, but I want to hear what you have to say.
0
Comment
Question by:Sylvania
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 2728339
Can't you sort them before putting them in your combo?
0
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 2728345
See this previous EE question: http://www1.experts-exchange.com/bin/Q.10244480
0
 
LVL 2

Expert Comment

by:Sage020999
ID: 2728410
If you choose not to sort them before putting them in the combo box, you may need to pad the numbers with zeros. Some thing like this would work if you don't mind the extra digits:



Function Pad(ByVal Item As String) As String

    If IsNumeric(Item) Then
   
      If InStr(Item, ".") <> 0 Then
          Do Until InStr(Item, ".") = 10
              Item = "0" & Item
          Loop
      Else
          Do Until Len(Item) >= 9
            Item = "0" & Item
          Loop
      End If
 
    End If

 
  Pad = Item
 
End Function
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 54

Accepted Solution

by:
nico5038 earned 60 total points
ID: 2728683
Start with checking for longest number.

Then move with (lets say it's 10):

strItem = right("          " & strYourvalue, 10)

before you do add the item to the combobox.

Take care the font is NOT proportional otherwise you'll see something like this:
     1
    10
   192
 12234
 12244

Success!

0
 
LVL 2

Expert Comment

by:Sage020999
ID: 2728719
Use this function to make your numbers proportional

Function Pad(ByVal Item As String) As String

    If IsNumeric(Item) Then
   
      If InStr(Item, ".") <> 0 Then
          Do Until InStr(Item, ".") = 10
              Item = " " & Item
          Loop
      Else
          Do Until Len(Item) >= 9
            Item = " " & Item
          Loop
      End If
   
    End If

   
  Pad = Item
   
End Function
0
 
LVL 54

Expert Comment

by:nico5038
ID: 2728817
Saga, proportionality isn't a property of a string and not of a number.
It has to do with a font. (The width of every single character can be different. A "m" is wide and a "i" is small)

When typing this comment the characters are NOT proportional. When viewing the result it's proportional!

Just copy and paste my sample in the input window and you can see the numbers are all right-aligned!
0
 
LVL 28

Expert Comment

by:AzraSound
ID: 2729133
heres a sort algorithm i wrote real quick.  Not super proficient but it gets the job done.


Option Explicit
Dim arr() As Integer

Private Sub Form_Load()
    Dim i As Integer
    ReDim arr(0 To Combo1.ListCount - 1)
    For i = 0 To Combo1.ListCount - 1
        arr(i) = Combo1.List(i)
    Next
    Call SortBox(arr)
End Sub


Function SortBox(tArr As Variant)
    Dim i As Integer
    Dim j As Integer
    Dim temp As Integer
    For i = 0 To UBound(arr)
        For j = i + 1 To UBound(arr)
            If tArr(j) < tArr(i) Then
                temp = tArr(i)
                tArr(i) = tArr(j)
                tArr(j) = temp
            End If
        Next
    Next
    Combo1.Clear
    For i = 0 To UBound(tArr)
        Combo1.AddItem tArr(i)
    Next
End Function
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now