Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Simple sort question 3

Status
Not open for further replies.

mstelmac

Technical User
Oct 22, 2003
53
US
Hello everyone, does anyone know the easiest way to sort (ascending) a variable that contains the values:
c001 c002 c004 c003

The values are delimited by whatever is needed, currently it is by space. Im learning that in VB script I can not just push them into an Array and use a "sort" command. You actually have to write the sort procedure (unlike PERL). I have found info on a quick sort and bubble sort but both seem complicated and Im having trouble following and determining what I really need.

thanks very much,
matt
 
It depends on the size, but putting them in an array and using a bubble or quick sort is your best bet. I learned a long time ago the metrics for each of those types of sorts, but I've since forgotten them and I am too lazy to look it up again. If you post code that you are having trouble with and explain the problem I'm sure you will get help on your specific issue.

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
A starting point:
myVar = "c003 c001 c004 c002"
myArr = Split(myVar)
For i = 0 To UBound(myArr) - 1
For j = i + 1 To UBound(myArr)
If myArr(i) > myArr(j) Then
tmp = myArr(j): myArr(j) = myArr(i): myArr(i) = tmp
End If
Next
Next
MsgBox myVar & vbCrLf & Join(myArr)

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Here's a QuickSort for you. The space is added to the front, so that a(0) can be used.

Code:
myVar = " " & "c003 c001 c004 c002"
a = Split(myVar)
Call QuickSort(1,UBound(a))
For i = 1 to UBound(a) : MsgBox a(i) : Next

Sub QuickSort(LeftSide, RightSide)
    Dim v, t, i, j
    If (RightSide>LeftSide) Then
        v=a(RightSide) : i=LeftSide-1 : j=RightSide : a(0) = v
        Do
            Do : i=i+1 :  Loop Until a(i)>=v
            Do : j=j-1 :  Loop Until a(j)<=v
            t=a(i) : a(i)=a(j) : a(j)=t
        Loop Until (j<=i)
        a(j)=a(i) : a(i)= a(RightSide) : a(RightSide)=t
        Call QuickSort(LeftSide,i-1)
        Call QuickSort(i+1,RightSide)
    End If
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top