Q:

Sorting in rails app

I created an app that gives points to students. I want to sort the points in descending order. This is my code.
<% @users.each do |user| %>
<li> <%= user.points%></li>
<%end%>

This code outputs the points from each student, but when I try to add sort! after user.points, it gives me an error. I am not sure where I should do this.

A:

Try this:
<% @users.sort_by{|u| u.points}.reverse.each do |user| %>
  <li> <%= user.points%></li>
<% end %>

Note: I have used reverese as you have mentioned I want to sort the points in descending order. But It's always good practise to assign data to object  in controller as per your requirement.
Do it in controller like:
@users = User.order(points: :desc) # this will fetch users in descending order on basis of points

then in view simply use:
<% @users.each do |user| %>
  <li> <%= user.points%></li>
<%end%>

for more info regarding Order

