|
-
Mar 12th, 2004, 03:57 PM
#1
Thread Starter
PowerPoster
RANDOM Sorting Objects in ArrayList
Please help me figure out how to sort this array list in a random sort.
Thanks
Code:
Private Sub BindBanners()
Dim ar As New ArrayList
Dim path As String = "images/banners/"
Dim item As New Telerik.WebControls.RotatorDataItem
item("BannerLocation") = path & "home.jpg"
ar.Add(item)
Dim item1 As New Telerik.WebControls.RotatorDataItem
item1("BannerLocation") = path & "about.jpg"
ar.Add(item1)
Dim item2 As New Telerik.WebControls.RotatorDataItem
item2("BannerLocation") = path & "contact.jpg"
ar.Add(item2)
Dim item3 As New Telerik.WebControls.RotatorDataItem
item3("BannerLocation") = path & "links.jpg"
ar.Add(item3)
Dim item4 As New Telerik.WebControls.RotatorDataItem
item4("BannerLocation") = path & "news.jpg"
ar.Add(item4)
Dim item5 As New Telerik.WebControls.RotatorDataItem
item5("BannerLocation") = path & "service.jpg"
ar.Add(item5)
Dim item6 As New Telerik.WebControls.RotatorDataItem
item6("BannerLocation") = path & "gallery.jpg"
ar.Add(item6)
Dim item7 As New Telerik.WebControls.RotatorDataItem
item7("BannerLocation") = path & "gallery2.jpg"
ar.Add(item7)
'Generate random value
Randomize()
Dim MyValue As Integer = CInt(Int((ar.Count * Rnd()) + 1))
ar.Sort(MyValue, ar.Count - 1, Nothing)
RadRotator1.DataSource = ar
RadRotator1.DataBind()
End Sub 'BindBanners
-
Mar 12th, 2004, 04:00 PM
#2
Member
im kind of new to VB so this is probably the worst possible way to accomplish what you want... But you could always do either a case statement, or an If-then-elseif statement for each random number and change the location of the variables in the array...
Its tedious, and i may actauly have no idea what your trying to accomplish, but it would work...i think :/
--Flac
Everything great once started with the words "That will never catch on, you shouldnt even bother" be great, go against the crowed, do something stupid, you might become famous.
-
Mar 12th, 2004, 05:09 PM
#3
Hi.
I guess you could make your own sorting class that implements IComparer.
In that you would create a function with 2 args. representing the 2 objects to be compared.
then just return a random number of -1,0,1.
-1 means X<Y
0 means X=Y
1 menas X>Y
When you sort you could pass a new instance of your comparerclass as an argument.
I haven't tried it on ArrayList before, but I have used the method of sorting all kinds of other objects, including my own classes.
Edit:
I have just done a few test's and it seems that returning random numbers in a comparerclass might not be as easy as I first expected Apperantly i relies on a "0" being returned at a certain time. I've never seen this behaviour when sorting normally, but If I return random numbers it throws an exception sometimes.
So it seems my "solution" might not be the right one, but at least I'm sure you can use the info about the comparerclass to something anyway.
Sorry I couldn't be of any more help.
Last edited by pax; Mar 12th, 2004 at 05:31 PM.
I wish I could think of something witty to put in my sig...
...Currently using VS2013...
-
Mar 12th, 2004, 05:15 PM
#4
Frenzied Member
Depending how how random you want, you can go the easy way or look up some good algorithm.
The easy way is to loop through all your elements, swapping randomly . i.e., if you have 8 total elements:
Swap #1 with 1,2,3,4,5,6,7 or 8
Swap #2 with 1,2,3,4,5,6,7 or 8
Swap #3 with 1,2,3,4,5,6,7 or 8
etc.
I'm no mathmatician, but if I understand what I've read, this becomes less random with a larger number of elements. I'm not smart enough to explain that, but it probably doesn't make a difference for "randomizing" banners.
Mike
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|