Solved

Combo Sort

Posted on 2000-04-18
7
532 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

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…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…
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…

730 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