Link to home
Start Free TrialLog in
Avatar of kunghui80
kunghui80Flag for Singapore

asked on

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
Avatar of EDDYKT
EDDYKT
Flag of Canada image

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
Avatar of kunghui80

ASKER

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
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


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...
ASKER CERTIFIED SOLUTION
Avatar of Martin Liss
Martin Liss
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi MartinLiss,

Thanks for help.

Regards,
kelvin