Solved

Combo Sort

Posted on 2000-04-18
7
531 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 70

Expert Comment

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

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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

831 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