Log inUsernamePassword
Log me on automatically each visit    
Register
Register
Log in to check your private messages
Log in to check your private messages
Visual Basic Forum for Visual Basic Programmers VB Forum Index » General

Post new topic   Reply to topic
sort array of labels ascending
View previous topic :: View next topic  
Author Message
Downtown
Newbie


Joined: 06 Dec 2003
Posts: 10

PostPosted: Dec 31st, 2003 11:29 AM    Post subject: sort array of labels ascending Reply with quote

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
View user's profile Send private message Send e-mail
Andir
Centurion


Joined: 21 Dec 2003
Posts: 184
Location: Chicago Area

PostPosted: Jan 1st, 2004 03:22 AM    Post subject: Reply with quote

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
View user's profile Send private message
Downtown
Newbie


Joined: 06 Dec 2003
Posts: 10

PostPosted: Jan 1st, 2004 09:10 AM    Post subject: sorting array Reply with quote

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
View user's profile Send private message Send e-mail
Andir
Centurion


Joined: 21 Dec 2003
Posts: 184
Location: Chicago Area

PostPosted: Jan 1st, 2004 09:54 AM    Post subject: Reply with quote

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 Smile

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
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Visual Basic Forum for Visual Basic Programmers VB Forum Index » General All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Visual Basic Forum runs phpBB | Forum Template © iOptional
VB Resources | SSL | Visual Basic