ruby - How to reorder activerecord array based on array of ids -


i have existing activerecord array, gotten following:

@posts = post.where(["created_at > ?", n_days.days.ago]) 

and order array based on value needs calculated in real-time multiple columns:

posts_array = posts.map { |p| [p.id, f(t, p.x,p.y)] } 

this gives array [[1,20],[2,11],[3,14], ...], sort based on function value give [[1,20],[3,14],[2,11],...], , order_array want: [1,3,2,...].

what best way reorder original @posts array based on new order_array [1,3,2,...]? don't want touch database again if possible, since information contained in @posts array.

is there straightforward way this?

if don't need posts_array array do

@posts = post.where(["created_at > ?", n_days.days.ago]) @posts.sort_by! { |p| f(t, p.x, p.y) } 

Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -