Item Reorder

Post any examples or modules that you want to share here
Perfect Human Interface
Posts: 643
Joined: Sun Mar 10, 2013 7:32 pm

Item Reorder

Post by Perfect Human Interface »

This is a simple Ruby code to reorder a number of items by shifting individual items up and down. Feed it the index number you want to move and then send a trigger to shift it either up or down.

Edit: I flipped the up/down triggers since it's more likely we're listing things from top to bottom rather than "up" being a numerically higher index.

Edit 2: I added a second method which may be more useful. This acts like a list of positions rather than a list of items, so it increments the values instead of shifting them in the array. So in this case the index would be the item and the value would be the order position, rather than the other way around. This makes it much easier to draw the position value of a given item (the other method is easier to draw the item value at a given position).
Attachments
Array Item Reorder 2b_PHI.fsm
(2.3 KiB) Downloaded 1112 times
Last edited by Perfect Human Interface on Mon Mar 16, 2015 9:22 pm, edited 4 times in total.
User avatar
Nubeat7
Posts: 1347
Joined: Sat Apr 14, 2012 9:59 am
Location: Vienna
Contact:

Re: Item Reorder

Post by Nubeat7 »

i used this methode for my FX order module

but for a D.R.Y. programmingstyle i wrote an array class methode for it (you can also write a "normal" methode) - you can find this also this in my array class methodes expansion module, or you just define it inside the ruby code..

when you trigger +1 or -1 for moving the item up and down, you only need to write the swap code once (D.R.Y.)

i personally also don't like it to feed an output into an input like you did with the selection, it easily could end up in a feedback error, so i normally try to avoid feedbacks like this if possible

after the selection is based on a user input i think its not bad when it stays at the last userinput so you can remember the original position of the item (for example, if you changed the wrong item)
Perfect Human Interface
Posts: 643
Joined: Sun Mar 10, 2013 7:32 pm

Re: Item Reorder

Post by Perfect Human Interface »

Nubeat7 wrote:i used this methode for my FX order module

but for a D.R.Y. programmingstyle i wrote an array class methode for it (you can also write a "normal" methode) - you can find this also this in my array class methodes expansion module, or you just define it inside the ruby code..

when you trigger +1 or -1 for moving the item up and down, you only need to write the swap code once (D.R.Y.)


Hi Nubeat. I actually had a peek at your FX Order schematic before writing this. Your reordering stuff was wrapped up in other stuff so I decided to write my own Ruby block to fit my own needs. I decided to share it because the bare component is flexible and seemed like it could be useful to others.

I also noted (after writing this) that you had written a swap method. Since the "long" version of the swap code is written exactly three times, writing and replacing with a method wouldn't have had any real benefit (I just used copy/paste anyways). However I do see the benefit in this practice, both in time saved and readability.

i personally also don't like it to feed an output into an input like you did with the selection, it easily could end up in a feedback error, so i normally try to avoid feedbacks like this if possible

after the selection is based on a user input i think its not bad when it stays at the last userinput so you can remember the original position of the item (for example, if you changed the wrong item)


The output was fed directly to the input only as example. So just disconnect it and it will function like you want. It's highly typical that if you want to move an item's position in a list you may want to move it more than one step, so the "newindex" output is provided so that a schematic can react automatically to the new ordering.
Perfect Human Interface
Posts: 643
Joined: Sun Mar 10, 2013 7:32 pm

Re: Item Reorder

Post by Perfect Human Interface »

Updated first post with an additional method.
tulamide
Posts: 2714
Joined: Sat Jun 21, 2014 2:48 pm
Location: Germany

Re: Item Reorder

Post by tulamide »

That's not really shifting nor swapping - but still an interesting approach to re-ordering.

Here's a little code optimization:

Code: Select all

def event i,v

   if i == 0 #set the array
      @order = v
      output @order
   end
   
   if i == "moveUp"
   
      #swap array elements
      n = (@index - 1) % @order.length
      @order[@index],@order[n] = @order[n],@order[@index]
      output 1, n
      
      #output array
      output @order
   end
   
   if i == "moveDown"
      
      #swap array elements
      n = (@index + 1) % @order.length
      @order[@index], @order[n] = @order[n], @order[@index]
      output 1, n
      
      #output array
      output @order
   end
end


And here's an example for real shifting:

Code: Select all

def event i,v

   if i == 0 #set the array
      @order = v
      output @order
   end
   
   if i == "moveUp"
      @order.insert(-1, @order.shift)
      output @order
   end
   
   if i == "moveDown"
      @order.insert(0, @order.pop)
      output @order
   end
end
"There lies the dog buried" (German saying translated literally)
Perfect Human Interface
Posts: 643
Joined: Sun Mar 10, 2013 7:32 pm

Re: Item Reorder

Post by Perfect Human Interface »

tulamide wrote:That's not really shifting nor swapping - but still an interesting approach to re-ordering.


Well, semantics... I think "shifting individual items up and down" is straightforward enough. It's not meant to shift the whole array, of course.

Here's a little code optimization:


Thanks! I'll update the first post with that.
User avatar
Nubeat7
Posts: 1347
Joined: Sat Apr 14, 2012 9:59 am
Location: Vienna
Contact:

Re: Item Reorder

Post by Nubeat7 »

i see a problem here when it comes to the array bounds the array gets rotated, to prevent this and move the selected item maximum to the end or the beginning of the array i did a bound check (also for the selection, so that the user cannot select an item which would be out of range)

and i wrote all (moving up and down) into one block
Attachments
ArrayReorder.fsm
(710 Bytes) Downloaded 1092 times
RJHollins
Posts: 1573
Joined: Thu Mar 08, 2012 7:58 pm

Re: Item Reorder

Post by RJHollins »

This is like a ' Brain Trust' at work 8-)

Educational opportunity for those [me] watching/reading.

Sincerely
Thank-you Gentleman.
tulamide
Posts: 2714
Joined: Sat Jun 21, 2014 2:48 pm
Location: Germany

Re: Item Reorder

Post by tulamide »

Nubeat7 wrote:i see a problem here when it comes to the array bounds the array gets rotated, to prevent this and move the selected item maximum to the end or the beginning of the array i did a bound check (also for the selection, so that the user cannot select an item which would be out of range)

and i wrote all (moving up and down) into one block


I once saw a clamp solution that was so elegant and readable, that I want to share it here:

Code: Select all

newId = [0, @id + @move, @order.length - 1].sort[1]


EDIT: You should never use a capital letter to start a variable!
"There lies the dog buried" (German saying translated literally)
Perfect Human Interface
Posts: 643
Joined: Sun Mar 10, 2013 7:32 pm

Re: Item Reorder

Post by Perfect Human Interface »

Nubeat7 wrote:i see a problem here when it comes to the array bounds the array gets rotated, to prevent this and move the selected item maximum to the end or the beginning of the array i did a bound check (also for the selection, so that the user cannot select an item which would be out of range)

and i wrote all (moving up and down) into one block


That's intended behavior. Once it reaches the end it wraps around to the beginning.

The bound check for the selected item would really only be important if you weren't managing the value internally.

Your example throws an error right away when I load it. I'm using 3.0.4; not sure if that's relevant.
Post Reply