Page 1 of 2

Item Reorder

Posted: Mon Mar 16, 2015 1:52 am
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).

Re: Item Reorder

Posted: Mon Mar 16, 2015 11:22 am
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)

Re: Item Reorder

Posted: Mon Mar 16, 2015 4:43 pm
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.

Re: Item Reorder

Posted: Mon Mar 16, 2015 8:07 pm
by Perfect Human Interface
Updated first post with an additional method.

Re: Item Reorder

Posted: Mon Mar 16, 2015 9:02 pm
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

Re: Item Reorder

Posted: Mon Mar 16, 2015 9:17 pm
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.

Re: Item Reorder

Posted: Mon Mar 16, 2015 10:49 pm
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

Re: Item Reorder

Posted: Mon Mar 16, 2015 11:18 pm
by RJHollins
This is like a ' Brain Trust' at work 8-)

Educational opportunity for those [me] watching/reading.

Sincerely
Thank-you Gentleman.

Re: Item Reorder

Posted: Mon Mar 16, 2015 11:33 pm
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!

Re: Item Reorder

Posted: Tue Mar 17, 2015 1:08 am
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.