Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to sorting list of numbers using VB?

Posted on 2004-10-21
6
Medium Priority
?
412 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
Comment
Question by:kunghui80
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 26

Expert Comment

by:EDDYKT
ID: 12370958
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
ID: 12371952
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
ID: 12372048
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 2

Author Comment

by:kunghui80
ID: 12377809
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 49

Accepted Solution

by:
Martin Liss earned 1000 total points
ID: 12405510
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
ID: 12470186
Hi MartinLiss,

Thanks for help.

Regards,
kelvin
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses

609 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