How to sorting list of numbers using VB?

Hi,

This is about sorting question, which i'm not very familiar with.
I've one array list of number, unsorted... for example.
100,1,10,21,5,7,8,101

How to create a VB application to read in a list of number from the users and sort it? (Max->Min) and (Min->Max). I've created two command buttons, one is (Max->Min) and another one is (Min->Max). When i click on the button, i need to sort it either from max. to min. or min. to max.

VB Environment
1 Text Field (for input), 1 Text Field (for result display), 2 Command Buttons.

If it's not logic to be read from text field, can i declare internally inside the coding?
eg. ArraryList="100,1,10,21,5,7,8,101" (This doest work)

How to implement for this? Thanks a lot.

Best regards,
Kelvin
LVL 2
kunghui80Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

EDDYKTCommented:
here is sort route

http://www.vb-helper.com/tut1.htm


to pass into rourtine

you can do


dim arr

arr = split(text1.text, ",")

pass arr into routine
0
kunghui80Author Commented:
Thanks for the resources, but how to apply in this case?
How to pass the list 100,1,10,21,5,7,8,101 into the sorting algorithm?

Sub Selectionsort (List() As Long, min As Integer, _
    max As Integer)
Dim i As Integer
Dim j As Integer
Dim best_value As Long
Dim best_j As Integer

    For i = min To max - 1
        best_value = List(i)
        best_j = i
        For j = i + 1 To max
            If List(j) < best_value Then
                best_value = List(j)
                best_j = j
            End If
        Next j
        List(best_j) = List(i)
        List(i) = best_value
    Next i
End Sub

Thanks for help.

Regards,
kelvin
0
EDDYKTCommented:
Try this


Option Explicit

Private Sub Command1_Click()
Dim arr

arr = Split("100,1,10,21,5,7,8,101", ",")
Selectionsort arr, 0, UBound(arr)
End Sub
Sub Selectionsort(List, min As Integer, max As Integer)
Dim i As Integer
Dim j As Integer
Dim best_value As Long
Dim best_j As Integer

    For i = min To max - 1
        best_value = CLng(List(i))
        best_j = i
        For j = i + 1 To max
            If List(j) < best_value Then
                best_value = CLng(List(j))
                best_j = j
            End If
        Next j
        List(best_j) = List(i)
        List(i) = best_value
    Next i
End Sub


0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

kunghui80Author Commented:
Hi, EDDYKT

I tried this, but the result only return either the maximum or minimum number. How to list down the whole list of numbers in order? e.g. 1,5,7,8,10,21,100,101 or 101,100,10,21,8,7,5,1?

Sorry, i really have no ideal how to implement this. Thanks for help...
0
Martin LissOlder than dirtCommented:
Private Sub cmdMaxToMin_Click()

    txtOutput = SortList(txtInput.Text, "MaxToMin")
   
End Sub

Public Function SortList(strValues As String, strDirection As String) As String

    Dim strNumbers() As String
    Dim lngSave As Long
    Dim lngIndex As Long
    Dim bSwapped As Boolean
   
    strNumbers = Split(strValues, ",")
    bSwapped = True
    Do Until Not bSwapped
        bSwapped = False
        For lngIndex = 0 To UBound(strNumbers) - 1
            If CLng(strNumbers(lngIndex)) > CLng(strNumbers(lngIndex + 1)) Then
                lngSave = strNumbers(lngIndex)
                strNumbers(lngIndex) = strNumbers(lngIndex + 1)
                strNumbers(lngIndex + 1) = lngSave
                bSwapped = True
                Exit For
            End If
        Next
    Loop
   
    If strDirection = "MinToMax" Then
        For lngIndex = 0 To UBound(strNumbers)
            SortList = SortList & strNumbers(lngIndex) & ","
        Next
    Else
        For lngIndex = UBound(strNumbers) To 0 Step -1
            SortList = SortList & strNumbers(lngIndex) & ","
        Next
    End If
   
    SortList = Left$(SortList, Len(SortList) - 1)

End Function

Private Sub Text2_Change()

End Sub


Private Sub cmdMinToMax_Click()

    txtOutput = SortList(txtInput.Text, "MinToMax")

End Sub


Usage: User types a list like "1,5,99,4,3" into txtInput and then clicks either of the command buttons. Output is shown in txtOutput (which probably should be a label.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
kunghui80Author Commented:
Hi MartinLiss,

Thanks for help.

Regards,
kelvin
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

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.