Look at Robert's suggestion for how to sort arrays compared to each 
other. His suggestions are usually (perhaps always) the best solution to 
the problem.

Here's his solution converted for your recent example:
arr2.sort_by {|o| terms.index {|a| a[:id] == o[:term_id] }}

My thinking was that you could handle the object creation differently, 
but that may not be suitable in your case. There's always the option to 
conjoin the objects after sorting them (see the Array#zip method) in 
order to keep them together when you continue onto the next task.
Using a sort just seems messy to me, because you're relying heavily on 
there being a matched pair in each case with no duplicates or missing 
objects. Still, it might be exactly the right thing to do in your 
scenario. There's more than one way to do it :)

-- 
Posted via http://www.ruby-forum.com/.