Rails sort_by method with two fields sorted in ascending order, one sort in descending order

I would like to do partial, which sorts by score, then by name (if several players have the same score).

Now I use this:

<%= render @players.sort_by { |p| [p.scored_vote(current_week), p.last_name] } %>

This works, but sorts the grades in ascending order, and I would like to sort them in descending order. How can I flip the sort order for evaluation, but not for the name that I would like to sort in ascending order?

Thank!

+3
source share
1 answer

You have nothing to do with partial ones. you are interested in the behavior of the sort_by method. In btw, this should solve your problem:

<%= render @players.sort_by { |p| [-p.scored_vote(current_week), p.last_name] } %>

+13
source

Source: https://habr.com/ru/post/1529595/


All Articles