Sort array returned by ActiveRecord by date (or any other column)

user94154 picture user94154 · Aug 14, 2009 · Viewed 69.2k times · Source

How can I sort an array returned by an ActiveRecord query by a created_at date column?

This occurs once the query has been executed.

Please don't tell me to do it in the query because I need this to happen in the view.

Answer

Chuck picture Chuck · Aug 14, 2009

Ruby includes support for sorting out of the box.

sorted = @records.sort_by &:created_at

However, this doesn't appear to have much to do with display and probably belongs in the controller.