Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 707
user94154 Avatar asked Aug 14 '09 15:08

user94154


People also ask

How do you sort an array in Ruby?

The Ruby sort method works by comparing elements of a collection using their <=> operator (more about that in a second), using the quicksort algorithm. You can also pass it an optional block if you want to do some custom sorting. The block receives two parameters for you to specify how they should be compared.

What is ActiveRecord?

Active Record is the M in MVC - the model - which is the layer of the system responsible for representing business data and logic. Active Record facilitates the creation and use of business objects whose data requires persistent storage to a database.

What is an ActiveRecord relation object?

The Relation Class. Having queries return an ActiveRecord::Relation object allows us to chain queries together and this Relation class is at the heart of the new query syntax. Let's take a look at this class by searching through the ActiveRecord source code for a file called relation.

What is ActiveRecord :: Base in Rails?

ActiveRecord::Base indicates that the ActiveRecord class or module has a static inner class called Base that you're extending. Edit: as Mike points out, in this case ActiveRecord is a module... ActiveRecord is defined as a module in Rails, github.com/rails/rails/tree/master/activerecord/lib/…


4 Answers

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.

like image 181
Chuck Avatar answered Oct 10 '22 07:10

Chuck


While Ruby Enumerable is awesome, ActiveRecord queries will actually return an ActiveRecord::Relation whose query will not have been evaluated yet (Lazy Loading) and can have the order method called on it to off-load this processing to the database where it will scale much better than an Enumerable based strategy.

Using Enumerable for sorting also confounds doing pagination in the database. There is nothing to prevent the order strategy from being applied in the view. However, I would tend to put this in scope on the model.

sorted = @records.order(:created_at)
like image 30
shulmang Avatar answered Oct 10 '22 06:10

shulmang


Just call sort on the collection, passing in the block of code which tells Ruby how you want it to sort:

collection.sort { |a,b| a.created_at <=> b.created_at }
like image 26
Bill D Avatar answered Oct 10 '22 06:10

Bill D


Please look at this one and also check complexities.

Model.all.sort_by{|m| m.created_at} #=> O(log n)

#versus

Model.order(“created_at DESC”) #=> O(1)
like image 13
Sajjad Murtaza Avatar answered Oct 10 '22 08:10

Sajjad Murtaza