[Gambas-user] A random sort of listview

Simonart Dominique simonart.dominique at ...11...
Tue Apr 7 14:30:48 CEST 2009


Simonart Dominique a écrit :
> Hi,
> 
> Hmm, did you run exactly this code? If so, there is 
> something wrong with the second FOR NEXT loop
> 
> jbskaggs a écrit :
>> 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 
>>
> 
> the right code is:
> 
> FOR i = 199 TO 0 STEP -1
>     ...
> NEXT

But if you prefer incremental loop, you could write:

FOR i = 0 TO 199
    A = Int(Rnd(i,200))
    SWAP ...
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
>>
> 
> Dominique Simonart
> 





More information about the User mailing list