[Gambas-user] A random sort of listview

jbskaggs jbskaggs at ...1871...
Tue Apr 7 01:55:08 CEST 2009


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
-- 
View this message in context: http://www.nabble.com/A-random-sort-of-listview-tp22919766p22919766.html
Sent from the gambas-user mailing list archive at Nabble.com.





More information about the User mailing list