Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails sort_by method with two fields one sorted ascending, one sorted descending

I would like to render a partial that sorts by score, then by name (if multiple players have the same score).

Right now I am using this:

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

This works, but it is sorting the scores in ascending order, and I would like to sort them in descending order. How can I flip the sort order for the score, but not for name, which I would still like to sort in ascending order?

Thanks!

like image 306
jackerman09 Avatar asked Oct 25 '13 01:10

jackerman09


1 Answers

You question has nothing to do with rendering partials. what you are interested in is the behaviour of sort_by method . In btw, this should solve your problem:

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

like image 186
Sahil Dhankhar Avatar answered Oct 13 '22 00:10

Sahil Dhankhar