Notifications
Clear all

[Closed] Multilistbox in descendent order

Hi,
i’d like to sort the selection of a multilistbox in descendent order:
example :
i have a list of 6 items, i select the 3 last, so i got :

mltbox.selection
#(4…6)

when i make a loop on this selection, the loop starts from 4 to 6,
i need to have the loop starting from 6 to 4

i know that “sort #()” does an ascendent order, i also found “amax” and “amin” and “qsort” but i don’t know how to use them.

this would be easy but i don’t know the method

thanks for some help

5 Replies

You can use a reversed loop, mening it will work from the last item to the first. Here is a sample code:


myArr = #("a","b","c","d","e","f")
for i = myArr.count to 1 by -1 do format "% 
" myArr[i]

Hope this helps!

thanks anton,

but i need to do this (a list of 6 items, i select 3 last)

[i]mltlbx.items = #(“a”,“b”,“c”,“d”,“e”,“f”) [color=PaleGreen]–this is my list of 6 items
local tmp_array = #(“a”,“b”,“c”,“d”,“e”,“f”) –a temporary array which contain the same values as the list (i need this array for other operations)

listArr = mltlbx.selection –array of selected items returned by the multilistbox
listArr = #(4…6) –it’s a bitarray

– i want to delete in tmp_array the items i selected in the list
– the item 6 in the list must delete the item 6 in tmp_array
for i in listArr(in descendent order) do deleteItem tmp_array i[/i][/color]

i can’t use a decrement by -1, because if i selected items 1, 3 and 6, the same items must be deleted in tmp_array (not 6, 5 and 4). Deleting items in tmp_array must be in descendent order, otherwise i got an error “out of boundary”…

maybe my method is not useful but i don’t know how to do it in another way

If I understand it right, you want to delete the selected multilistbox items from the temp array right? If so, here is a small example:


rollout rlt_test "Test"
(
 multilistbox mls_test "Items to be deleted: "
 button btn_delete "Delete from tmpArr" width:200 height:30
 
 local myArr = #("a","b","c","d","e","f")
 local tmpArr = myArr
 
 on rlt_test open do
 (
  mls_test.items = myArr
 )
 
 on btn_delete pressed do
 (
  indexToDelete = mls_test.selection as array
  for i = indexToDelete.count to 1 by -1 do deleteItem tmpArr indexToDelete[i]
  format "% 
" tmpArr
 )
)
createdialog rlt_test 300 200

It works just the first time but you’ll get the idea.

that’s it, you understood ! and the method works perfectly.
i can now go further in my script.

i didn’t think that i could decrement by -1, but i didn’t use this :
deleteItem tmpArr indexToDelete[i]

i understand the process now

Thanks a lot Anton

No problem. You need to decrement when deleting stuff from arrays otherwise it will error out.
Glad that it helped!