I have an existing activerecord array, gotten by the following:
@posts = Post.where(["created_at > ?", n_days.days.ago])
And I would like to order this array based on a value that needs to be calculated in real-time from multiple columns:
posts_array = posts.map { |p| [p.id, f(t, p.x,p.y)] }
This gives an array like [[1,20],[2,11],[3,14], ...], which I then sort based on the function value to give [[1,20],[3,14],[2,11],...], and then ultimately get the order_array that I want: [1,3,2,...].
What is the best way to then reorder the original @posts array based on the new order_array [1,3,2,...]? I don't want to touch the database again if possible, since the information is already contained in the @posts array.
Is there a straightforward way to do this?
sortfollowed bymap)