jbskaggs a écrit : > > I came up with a way to random sort listview. (Iuse this for random > shuffling card slots on games) But it isnt efficient some pointers if you > dont mind? > > I use two listviews one to sort from and one to sort to: > > I first copy all the listitems from list 1 to list 2 EXCEPT the key for > listview2 items = the integer from my for next loop > > eg > for i = 0 to listview1.count -1 step 1 > listview2.add(i, listview1.item.text) > next > > then with a for next loop based on the length of list1 I copy the > listview.item key to a string A and listview.item.text to second string B > > Then I calculate a random number the count of listview2 and add that to a > integer C > thenadd it to listview2 > listview2.add("n"&A, B,,C) "n"&A creates a key of n1, n2, etc ... > > > AFter the loop ends > > I run a second loop > > and run > for i = 0 to listview1.count -1 step 1 > listview.moveto(i) > listview2.item.delete > next > > This gives me a random sorted list in viewlist2 of viewlist1 with no > duplicates etc- > > But is there a more efficient way of doing this? > > here is total code: > > PUBLIC SUB button3_click() > DIM a AS String > DIM d AS Integer > DIM e AS Integer > DIM i AS Integer > DIM c AS Integer > listview1.Clear 'clears the list > FOR i = 0 TO 199 STEP 1 'number of items to add > listview2.Add(i, "Slot" & i) 'adds items to sort to list > NEXT 'next item > i = 0 > > FOR i = 0 TO 199 STEP 1 'number of items > e = 0 > listview1.MoveTo(i) 'goto item > a = listview1.Item.Text 'get text > PRINT a > d = listview1.Key 'get key > PRINT "old key", d > listview1.Item.Delete 'delete item (the cut part of cut and paste) > e = Round(Rnd(200)) ' make sure old key doesNOT equal NEW Key > PRINT e, "<<<< the randomimzed number" > > IF e >= 200 THEN e = 199 > listview2.Add("n" & d, "n" & a,, e) 'adds cut items after randomly > chosen item > listview3.Add("n" & d, "n" & a) > PRINT "new key", e > NEXT 'next item down > > FOR i = 0 TO 199 STEP 1 'number of items > listview2.MoveTo(i) > listview2.Item.Delete > NEXT > > ValueBox1.value = listview1.Count > END > > JB SKaggs Hi,
Let say you want to populate an array with 0-199 randomly (please note that I write this directly, so check carefully the syntax) dim myArray as integer[200] dim A as integer dim i as integer FOR i=0 to 199 A=int(Rnd(200-i)) ' I think we could use a Swap instruction ' instead of the 2 instructions below myArray[i]=A myArray[A]=i NEXT That's all If these numbers was the keys of the initial Listview you just have to populate the second Listview in the new order Hope this help Dominique Simonart ------------------------------------------------------------------------------ This SF.net email is sponsored by: High Quality Requirements in a Collaborative Environment. Download a free trial of Rational Requirements Composer Now! http://p.sf.net/sfu/www-ibm-com _______________________________________________ Gambas-user mailing list Gambas-user@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/gambas-user