
Sort Array of integers? VB5 Pro Ed.(sp2)
Quote:
> I need to be able to sort an array of variables (integers) so that I can
>print them as a string in the sorted order. Can anyone please help with the
>sorting?
>Thanks,
>--
>Tony Freeman
Hi Tony,
First of all, I'm a VB3 user, so I hope the following will be of use.
The principle I've used is to read, write or generate an unsorted
array and copy the array to a SORTED list box, and then overwrite the
contents of the listbox onto the original array. The code is
deliberately as longwinded as possible so that you can follow what's
going on.
Paste the following code into the (general) declarations of a default
form.
THEN paste a Command Button and a List Box onto the form
- you MUST set the List Box's Sorted Property to TRUE!
--------------------------------------
Dim a(0 To 9) As Integer
Sub Command1_Click ()
'Start Random Number Generator
Randomize
'Create Random Integers
'and copy to array
For i = 0 To 9
a(i) = Int(Rnd * 20)
Next i
'Print array onto form
Print "Unsorted Integers"
For i = 0 To 9
Print a(i)
Next i
'Copy array to SORTED list box
'Note: the format$ is important.
'The listbox is normally set to Visible = FALSE
For i = 0 To 9
Next i
'Read the list box and re-copy to array
For i = 0 To list1.ListCount - 1
a(i) = list1.List(i)
Next i
'Visual seperator
Print
'Print sorted array
Print "Sorted Integers"
For i = 0 To 9
Print a(i)
Next i
End Sub
------------------------------------
Besta luck,
----------------
P133 - 16 meg.
Mail - Eudora
News - Free Agent
-----------------
My fellow Webians,
Ask NOT what your web can do for you,
Ask what YOU can do for your web.
-----------------