We help IT Professionals succeed at work.
Get Started

Using Bubblesort to sort numbers

258 Views
Last Modified: 2010-04-30
I have the following code, and it is sorting numbers as if they were text. For example:

12
2
399922
4000
5

I need it to sort based on the value:
2
5
12
4000
399922

Here is my code, can someone show me how i can get it to work correctly:

Private Sub CommandButton3Sort_Click()
'http://www.dailydoseofexcel.com/archives/2004/05/24/sorting-a-multicolumn-listbox/
Dim i As Long, j As Long, LbList As Variant, K As Long
Dim sTemp As String, sTemp2 As String, sTemp3 As String, sTemp4 As String, sTemp5 As String, sTemp6 As String, sTemp7 As String
    'Store the list in an array for sorting
    LbList = Me.ListBox3.List
    K = ComboBox3.ListIndex
    'Bubble sort the array on the first value
    For i = LBound(LbList, 1) To UBound(LbList, 1) - 1
        For j = i + 1 To UBound(LbList, 1)
            If LbList(i, K) > LbList(j, K) Then
                'Swap the first value
                sTemp = LbList(i, 0)
                LbList(i, 0) = LbList(j, 0)
                LbList(j, 0) = sTemp
               
                'Swap the second value
                sTemp2 = LbList(i, 1)
                LbList(i, 1) = LbList(j, 1)
                LbList(j, 1) = sTemp2
               
                'Swap the third value
                sTemp3 = LbList(i, 2)
                LbList(i, 2) = LbList(j, 2)
                LbList(j, 2) = sTemp3
               
                'Swap the fourth value
                sTemp4 = LbList(i, 3)
                LbList(i, 3) = LbList(j, 3)
                LbList(j, 3) = sTemp4
               
                'Swap the fifth value
                sTemp5 = LbList(i, 4)
                LbList(i, 4) = LbList(j, 4)
                LbList(j, 4) = sTemp5
               
                'Swap the sixth value
                sTemp6 = LbList(i, 5)
                LbList(i, 5) = LbList(j, 5)
                LbList(j, 5) = sTemp6
               
                'Swap the seventh value
                sTemp7 = LbList(i, 6)
                LbList(i, 6) = LbList(j, 6)
                LbList(j, 6) = sTemp7
            End If
        Next j
    Next i
    'Remove the contents of the listbox
    Me.ListBox3.Clear
    'Repopulate with the sorted list
    Me.ListBox3.List = LbList
End Sub

Comment
Watch Question
CERTIFIED EXPERT
Top Expert 2008
Commented:
This problem has been solved!
Unlock 1 Answer and 4 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE