Solved

# Sorting an Array of Strings

Posted on 2002-05-17
327 Views
I need to sort an array of strings - the fastest way possible.

I've tried bubblesort, but it's too slow.

Can someone send me some decent code which I can use.

A little explanation of what's happening would be great too,

thanks,

Davey.
0
Question by:DaveyByrne

LVL 14

Expert Comment

Well I think you could populate the listbox with the array you have.  Make sure to make sort propery of Listbox to True.  Make the listbox hidden.

The snippet is given

Dim arr1(4) As String  ' Given Array
Dim arr2(4) As String  ' Sorted Array

arr1(0) = "M"
arr1(1) = "G"
arr1(2) = "H"
arr1(3) = "S"
arr1(4) = "A"

For i = 0 To UBound(arr1)
Next i

' Getting the sorted array
For i = 0 To (List1.ListCount - 1)
arr2(i) = List1.List(i)
Next i

For i = 0 To UBound(arr2)
Msgbox arr2(i)
Next i
0

LVL 1

Accepted Solution

falconew earned 100 total points
0

LVL 69

Expert Comment

Beside of arrays, have you tried :

. an invisible comboobx (setting the Sorted property to True only when all the data have been loaded)

. an in-memory ADO recordset that can be sorted too.
0

LVL 26

Expert Comment

How many string are you going to sort.
additem for listbox is slow compare to use listview
0

LVL 4

Expert Comment

Here is function I found.  I have not tried it.

InsertSortAscending

Print
Email

Function InsertSortAscending(ValueList As Variant) As Variant
'#######################################
'     ##############################
' InsertSortAscending
'
' This function recieves a single dimens
'     ional array of any datatype
' that is not an object, and returns an
'     array which has been sorted
' in ascending order. It uses a standard
'     insert sort algorithm without
' data partitioning. It is relatively fa
'     st, and can sort 1000 4-digit
' integers in 1 second, and 1000 5-chara
'     cter strings in 1 second when
' in P-Code.
'
' Usage:
'Dim ArrayToBeSorted() as string
'Dim SortedArray as Variant
'SortedArray = InsertSortAscending(Array
'     ToBeSorted)
'
' Written by: Andy Carrasco (gabrielw@ix
'     .netcom.com) 3/3/97
'#######################################
'     ##############################
Dim RipVal As Variant
Dim RipOrdinal As Long
Dim RipDescent As Long
Dim PrivateBuffer As Variant
Dim Placed As Boolean
Dim X As Long
Dim Y As Long
PrivateBuffer = ValueList
'Ok, we start at the second position in
'     the array and go
'from there
RipOrdinal = 1
RipDescent = 1

For Y = 1 To UBound(PrivateBuffer)
RipVal = PrivateBuffer(Y)
If Y <> 1 Then RipDescent = Y

Do Until Placed

If PrivateBuffer(RipDescent - 1) >= RipVal Then
RipDescent = RipDescent - 1

If RipDescent = 0 Then

For X = Y To RipDescent Step -1
If X = 0 Then Exit For
PrivateBuffer(X) = PrivateBuffer(X - 1)
Next X
PrivateBuffer(RipDescent) = RipVal
Placed = True
End If
Else
'shift the array to the right

For X = Y To RipDescent Step -1
If X = 0 Then Exit For
PrivateBuffer(X) = PrivateBuffer(X - 1)
Next X
'insert the ripped value
PrivateBuffer(RipDescent) = RipVal
Placed = True
End If
Loop
Placed = False
Next Y

0

LVL 4

Expert Comment

as falconew saiys, thge quicksort algorithm would be ideal as long as there are only a few (less than a thousand or so) strings.
There are many, many types of sort algorithm, have a look on the net, you might have to re-write something from another language tho'
0

LVL 4

Expert Comment

PS, as Alexpert says - I have been known to use a hidden listbox in the past......disgusting habit, but it's remarkably quick... :)
0

LVL 2

Expert Comment

I had forgotten how offensive VB was... hidden list boxes for sorting strings? I don't know.

The reason why such an absurd method may well be quicker than the quicksort algorithm (which is by far the best) is because you're swapping strings around all over the place.

Oh it is so simple in C. You just swap your pointers around :o) (which is, by the way, what the listbox will do)

In VB, create an array of indeces and sort the indeces using the quicksort method.

Ie:

Dim MyStrings(1 to 100) as string
Dim i(1 to 100) as integer

For n%=1 to 100
i(n%) = n%
Next

[Implement quicksort method, but refer to strings by MyStrings(i(INDEX)) and when you have to swap two strings in the algorithm - just swap the i(INDEX) numbers]

0

LVL 1

Author Comment

Since you were the first to give me the code, and I ended up using it - you've got the points.

Thanks to everyone for their answers,

Dave.
0

## Featured Post

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…