Order a collection as DESC

Ruby on-RailsRuby

Ruby on-Rails Problem Overview


<%= render :partial => 'event', :collection => @events.sort_by(&:event_at)%>

This code shows a collection ordered as ASC, but i want to order this collection as DESC.

How can I achieve this?

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

Like described on http://guides.rubyonrails.org/active_record_querying.html

@events.order(event_at: :desc)

Solution 2 - Ruby on-Rails

Even better, you can set a scope to sort your event and use it in your render.

In your Event model:

scope :desc, order("events.event_at DESC")

If you use Rails3, in your view you can simply do:

<%= render @events.desc %>

Solution 3 - Ruby on-Rails

In Rails 3 the correct syntax is:

<%= render :partial => 'event', :collection => @events.order(:event_at).reverse_order %>

Solution 4 - Ruby on-Rails

You can just reverse the sorted collection:

<%= render :partial => 'event', :collection => @events.sort_by(&:event_at).reverse %>

but as Yannis says you are better off sorting as you fetch things from the database ideally.

Solution 5 - Ruby on-Rails

Depending of the kind of object you have, you will have different ways of doing the sort function.

If your object is an ActiveRecord, you can do it the following way:

@events.order('events.event_at DESC')

This will add an ORDER clause to your SQL query, sorting the entries before you retrieve them from the database.

The second solution is slower, as you're sorting your entries in ruby.
But if you're manipulating an array of objects, it's your only solution.

@events.sort {|a,b| b.event_at <=> a.event_at }

This will loop through all the events, checking each one of them for the biggest one with the <=> method.

You can also see the sort documentation on Enumerables.

Solution 6 - Ruby on-Rails

I wanted to display a league table and order by points desc. After trying out several unsuccesful methods this is what worked for me in this instance. I added this line to my controllers index method.

@teams = Team.all.order(points: :desc)

Solution 7 - Ruby on-Rails

You can do this by using desc method with parameter.

See below example

@events.desc(:event_at)

This will give you @events in descending order of event_at field.

Thanks.

Solution 8 - Ruby on-Rails

In views/sources/show.html.erb the following <%= render @source.docs.order(page_no: :asc) %> works in Rails 6.

This calls _doc.html.erb which contains

<li>
  <%= doc.content %> (p. <%= doc.page_no %>) 
  <span class="btn btn-outline-success btn-xs"><%= link_to 'Show', doc %></span>
  <span class="btn btn-outline-warning btn-xs"><%= link_to 'Edit', edit_doc_path(doc) %></span>
</li>

Rails magic! An ordered list containing selected fields of docs that reference a source. And can click to edit or view the entire doc.

Thank you. One of the answers here pointed me in the sorting part.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSoroush HakamiView Question on Stackoverflow
Solution 1 - Ruby on-RailsFrancisco DCView Answer on Stackoverflow
Solution 2 - Ruby on-RailsYannisView Answer on Stackoverflow
Solution 3 - Ruby on-RailsJosephLView Answer on Stackoverflow
Solution 4 - Ruby on-RailsShadwellView Answer on Stackoverflow
Solution 5 - Ruby on-RailsDamien MATHIEUView Answer on Stackoverflow
Solution 6 - Ruby on-RailsPeter DawsonView Answer on Stackoverflow
Solution 7 - Ruby on-RailsAmol UdageView Answer on Stackoverflow
Solution 8 - Ruby on-RailsGregView Answer on Stackoverflow