SQLTerminal.app
now available

XPath.app
now available
Want fresh tech tips in your inbox?

Descending Sort By in Model For Active Record Hash on Created_at attribute

       

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 w/ #active record #array #created_at #sort_byruby on rails

Apps I've Built

XPath Expression Editor
Practice and improve your XPath skills with XPath Editor
Click to buy on the App Store
Photo Location Changer
Easily change the location on your photos and videos
Click here for more info about the app
Photo Date Changer
Easily change the dates and times on your photos and videos
Click here for more info about the app
VocabReminder
English dictionary with notifications so you won't forget what you're studying!
Click here for more info about the app
VocabQuiz
The app that quizzes and scores you on your vocabulary!
Click here for more info about the app