Solved

Combo Sort

Posted on 2000-04-18
7
527 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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

758 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

23 Experts available now in Live!

Get 1:1 Help Now