[Gambas-user] A random sort of listview

jbskaggs jbskaggs at ...1871...
Sat Apr 11 23:45:46 CEST 2009


Okay I thought this was worked out but its still not working- I always end up
losing the last two list items.

Here is the console output from my print statements:
This first list is printing i, a from this command SWAP myArray[i],
myArray[a]

21      21
20      13
19      0
18      13
17      11
16      4
15      9
14      12
13      4
12      2
11      6
10      2
9       4
8       0
7       5
6       1
5       3
4       3
3       0
2       0
1       0
0       0

This list shows where listview1 is being copied into listview2:

listview1.item.key      21       counter i:     21       total count:   22
listview1.item.key      13       counter i:     20       total count:   22
listview1.item.key      13       counter i:     19       total count:   22
listview1.item.key      20       counter i:     18       total count:   22
listview1.item.key      11       counter i:     17       total count:   22
listview1.item.key      4        counter i:     16       total count:   22
listview1.item.key      9        counter i:     15       total count:   22
listview1.item.key      12       counter i:     14       total count:   22
listview1.item.key      16       counter i:     13       total count:   22
listview1.item.key      2        counter i:     12       total count:   22
listview1.item.key      6        counter i:     11       total count:   22
listview1.item.key      14       counter i:     10       total count:   22
listview1.item.key      18       counter i:     9        total count:   22
listview1.item.key      19       counter i:     8        total count:   22
listview1.item.key      5        counter i:     7        total count:   22
listview1.item.key      5        counter i:     6        total count:   22
listview1.item.key      3        counter i:     5        total count:   22
listview1.item.key      7        counter i:     4        total count:   22
listview1.item.key      8        counter i:     3        total count:   22
listview1.item.key      15       counter i:     2        total count:   22
listview1.item.key      10       counter i:     1        total count:   22
listview1.item.key      17       counter i:     0        total count:   22

here is a screen shot of the two lists: listview1 is on the left, you will
notice that the last two items are not on the random sorted list on the
right.

http://www.nabble.com/file/p23005690/ScreenshotBug.png 


Here is my code:

PUBLIC SUB button4_click()
DIM myArray AS Integer[ListView1.count]
DIM a AS Integer
DIM i AS Integer
DIM c AS String

FOR i = 0 TO ListView1.count - 1 STEP 1
 myArray[i] = i 
NEXT 

FOR i = ListView1.count - 1 TO 0 STEP -1
a = Int(Rnd(i + 1))
SWAP myArray[i], myArray[a]
PRINT i, a
NEXT 

FOR i = ListView1.count - 1 TO 0 STEP -1 ' write items in listview2 -<<<<< I
have also tried running this counting up as well but it changed nothing.

listview1.MoveTo(myArray[i])
TRY c = listview1.Item.Key
TRY PRINT "listview1.item.key ", c, " counter i: ", i, " total count: ",
listview1.Count
TRY listview2.add(c, listview1.item.text) 
NEXT 
'fGlobal.show
'FRolldice.Show

'ME.Hide

END


Sincerly JB SKaggs



Simonart Dominique wrote:
> 
> 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
>> 
> 
> 
> ------------------------------------------------------------------------------
> 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-tp22919766p23005690.html
Sent from the gambas-user mailing list archive at Nabble.com.





More information about the User mailing list