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?
 
Martin LissConnect With a Mentor Older 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
 
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
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
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
 
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
 
kunghui80Author Commented:
Hi MartinLiss,

Thanks for help.

Regards,
kelvin
0
All Courses

From novice to tech pro — start learning today.