| View previous topic :: View next topic |
| Author |
Message |
Downtown Newbie
Joined: 06 Dec 2003 Posts: 10
|
Posted: Dec 31st, 2003 11:29 AM Post subject: sort array of labels ascending |
|
|
I have an control array of 10 labels. I use the ramdomize statement to get numbers from 1-80 into the labels. Then print the captions to the form.
I would like to sort the array's in ascending order before printing to the form. I had a simialar working program some years back and have lost the code.
Thanks in advance,
Downtown _________________ We can not change the past, the best we can hope for is to influence the future. |
|
| Back to top |
|
Andir Centurion

Joined: 21 Dec 2003 Posts: 184 Location: Chicago Area
|
Posted: Jan 1st, 2004 03:22 AM Post subject: |
|
|
There are several ways...and this is probably the long way, but bear with me.
| Code: |
Private Sub Form_Click() ' or whatever procedure you want to sort during
Randomize Timer
'Create a second array for storing the values
Dim storArry(9)
' and populate the numbers
For i = 0 To 9
storArry(i) = Int(Rnd * 80) + 1 'this gives integers from 1 to 80
Next i
'Sort the array, this is where you can do this several ways
' One thing to note...this accepts duplicate numbers, the sort will work with them.
Do
isSorted = True
For i = 0 To 8
If storArry(i) > storArry(i + 1) Then
isSorted = False
Swap storArry(i + 1), storArry(i)
End If
Next i
Loop Until isSorted
' Do another loop to set your labels equal to the storArry() values.
End Sub
Private Sub Swap(ByRef Value1 as Variant, ByRef Value2 as Variant)
Dim Temp As Variant
Temp = Value1
Value1 = Value2
Value2 = Temp
End Sub
|
I'm pretty sure this is not the best sort method but it works. Since your only dealing with 10 labels, it should work just fine.
edit: Dropped in a simple Sub procedure I like to keep handy...Swap(). It's quite useful if you find your swapping info all the time. _________________ If you happen to see little people sitting on your desk...don't tell anyone or they might think your crazy too.
Last edited by Andir on Jan 1st, 2004 03:31 AM; edited 1 time in total |
|
| Back to top |
|
Downtown Newbie
Joined: 06 Dec 2003 Posts: 10
|
Posted: Jan 1st, 2004 09:10 AM Post subject: sorting array |
|
|
Thanks for the information! i will try it and see how I get along. ?? Dim storArry(9)
could I change this to sortArray(9)
Also I could be confused with isSorted = False
What is the is in isSorted
Thanks in advance,
Downtown _________________ We can not change the past, the best we can hope for is to influence the future. |
|
| Back to top |
|
Andir Centurion

Joined: 21 Dec 2003 Posts: 184 Location: Chicago Area
|
Posted: Jan 1st, 2004 09:54 AM Post subject: |
|
|
Sure, you can change it to sortArray, but you'll need to change all of them. You know that of course...
isSorted is a boolean variable I use to determine if the array is sorted. You can DIM it if you like as a boolean variable, which is a good practice, just something I'm not used to doing yet
Look at it this way..the Do/Loop I set the isSorted = True because if I get all the way through each instance of the For/Next loop and don't have to swap values, then the array "isSorted". The If statement checks this for each value in the array. If I swap one time, isSorted changes to False, and it will run the loop again because the "Loop Until isSorted" will loop until isSorted = True. Make sense? _________________ If you happen to see little people sitting on your desk...don't tell anyone or they might think your crazy too.
Last edited by Andir on Jan 1st, 2004 10:00 AM; edited 1 time in total |
|
| Back to top |
|
|