Written by Sean Behan on Sun Jun 17th 2012

If you have a couple collections from the database and you want to sort it without the help of Active Record, take a look at the sort_by method on Array type. I've used this before when I have a couple of collections which are slightly different but I need them in a chronological order.

@posts_group_a = Post.find :all, :conditions => ["user_id = ?", current_user.id] @posts_group_b = Post.find :all, :conditions => ["user_id = ?", friend_user.id]

merge the two arrays here

@posts = @posts_group_a + @posts_group_b

notice the "-" is for descending order and the "to_i" casts the date time to an integer (required)

@posts.sort_by {|post| - post.created_at.to_i}

Tagged with..
#active record #array #created_at #sort_by #Ruby on Rails

Just finishing up brewing up some fresh ground comments...