# Combo Sort

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
Direct help would be appreciated rather then long workarounds, but I want to hear what you have to say.
###### Who is Participating?

x

Commented:

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

Senior .Net ConsultantCommented:
Can't you sort them before putting them in your combo?
0

Senior .Net ConsultantCommented:
See this previous EE question: http://www1.experts-exchange.com/bin/Q.10244480
0

Commented:
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

End Function
0

Commented:
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

End Function
0

Commented:
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

Commented:
heres a sort algorithm i wrote real quick.  Not super proficient but it gets the job done.

Option Explicit
Dim arr() As Integer

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)
Next
End Function
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.