[Gambas-user] A random sort of listview

jbskaggs jbskaggs at ...1871...
Tue Apr 7 06:36:34 CEST 2009


using your suggestions and the swap command vs manual swapping this is my
code:

public b as string  'optional string used to hold the array data for file or
split later

PUBLIC SUB button4_click() ' initialize array
DIM myArray AS Integer[200]
DIM a AS Integer
DIM i AS Integer

FOR i = 0 TO 199 STEP 1 'add array items 
 myArray[i] = i 
NEXT 

FOR i = 0 TO 199 STEP 1 ' random swap array items
a = Int(Rnd(i + 1))
SWAP myArray[i], myArray[a]
NEXT 


FOR i = 0 TO 199 STEP 1 ' write items in listview2
listview1.MoveTo(myArray[i])
c = listview1.Item.Key
listview2.add(c, listview1.item.text) 
NEXT 



FOR i = 0 TO 199 STEP 1 'optional step to write array as a string for file
or whatever use
b &= "slot" & myArray[i] & ","
NEXT 
PRINT b
END

PUBLIC SUB Button1_Click() 'an optional way to load the array values from
the variable b and / or a file if b was file loaded
DIM egg AS String[]
DIM c AS String
 listview1.Clear
 egg = Split(b, ",")
 FOR EACH c IN egg 
TRY listview1.Add(c, c) 
IF ERROR THEN RETURN 
NEXT 
IF listview1.Count >= 200 THEN RETURN 
END

JB Skaggs


Simonart Dominique wrote:
> 
> Simonart Dominique a écrit :
>> 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
>> 
>> 
> Well, never write directly like that!!
> I make some mistake in my sample, here is a new one:
> 
> DIM myArray AS Integer[200]
> DIM A AS Integer
> DIM i AS Integer
> 
> 'initialize the array
> FOR i=0 TO 199
>     myArray[i]=i
> NEXT
> 
> 'Randomize the order of the array
> FOR i = 199 TO 0 STEP -1
>     A = Int(Rnd(i + 1))
>     SWAP myArray[i], myArray[A]
> NEXT
> 
> 
> ------------------------------------------------------------------------------
> 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 at lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/gambas-user
> 
> 

-- 
View this message in context: http://www.nabble.com/A-random-sort-of-listview-tp22919766p22922075.html
Sent from the gambas-user mailing list archive at Nabble.com.





More information about the User mailing list