Assuming that you have to sort on title
and release_date
fields in movies
table.
You can perform the sorting at database level itself as below:
In Rails 4.x:
Below will sort all the movie records with title and release_date in ascending order(as default).
def index
@movies = Movie.order(:title, :release_date)
end
If you want to change the order, you can specify as asc
or desc
as:
def index
@movies = Movie.order(title: :asc, release_date: :desc)
end
In Rails 3.x:
def index
@movies = Movie.all(:order => "title ASC, release_date ASC")
end
If you want to change the order, you can specify as DESC
in the above case.