Sorting Arrays

JCarmic

Registered User.
Local time
Today, 14:53
Joined
Feb 12, 2001
Messages
14
Is there an in-built function or has anyone written a function to perform a sort on a one-dimensional array
 
I don't think there is an inbuilt function, but what you need to write is a "bubble sort".

The technique is simple. You repeatedly search through the array swapping contents of variables if they are higher (descending sort) or lower (ascending sort). Whenever you make a swap you set a switch variable on telling you that a swap has been made. Each time you search the array you set the switch OFF to begin. As soon as you do a search without making a swap, and the switch remains off, you know your array is sorted.

Something like (this is not tested)

Const NumberOfElements = 100
Dim MyArray(NumberofElements) 'this is a dynamic array

(Do something to populate the array with values)

Sub BubbleSort
Dim ISwapped as boolean, SwapVar

SortMe:
ISwapped = False

For I = 2 to NumberOfElements
If MyArray(I) < MyArray (I-1) then 'compare < ascending sort: compare > descending sort
SwapVar = MyArray(I)
MyArray(I)=MyArray (I-1)
MyArray(I-1)=SwapVar
ISwapped = True
End If
Next I

If ISwapped = False then Exit Sub else goto SortMe

End Sub

Get the idea? Hope this does it for you. If you are a No-GoTo programmer then you will have to loop into a subroutine or something. Cheers. DavidF

[This message has been edited by David Fletcher (edited 05-11-2001).]
 
heres another idea....if your populating the array with a recordset...just sort the recordset via SQL (ie.. SELECT tabel1.* FROM table1 ORDER BY Customer)
 

Users who are viewing this thread

Back
Top Bottom