Solved

How to sorting list of numbers using VB?

Posted on 2004-10-21
393 Views
Last Modified: 2010-08-05
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
0
Question by:kunghui80
    6 Comments
     
    LVL 26

    Expert Comment

    by:EDDYKT
    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
     
    LVL 2

    Author Comment

    by:kunghui80
    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
     
    LVL 26

    Expert Comment

    by:EDDYKT
    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
     
    LVL 2

    Author Comment

    by:kunghui80
    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
     
    LVL 44

    Accepted Solution

    by:
    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
     
    LVL 2

    Author Comment

    by:kunghui80
    Hi MartinLiss,

    Thanks for help.

    Regards,
    kelvin
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
    The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
    Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
    Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

    856 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

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now