9

I want to be able to rank 12 teams in a list according to 4 different criteria.

I will have a db table called Teams and each team will have various columns like matches_won, matches_lost, etc...

I want to display them in order based on matches_won. This is easy. However, if two teams are tied in matches_won then I must check whether they have played each other and who won. If they have the same matches_won AND have not played each other, their records are compared on two other criteria and ranked by that. I think if I can figure out how to do one additional condition to the sort that I will be able to figure out how to do the others. So for brevity's sake, I won't detail them here.

I don't know how to do this multi-level sorting in Rails.

I have thought maybe adding a 'rank' column in the db table and before_save function that ranks them going into the db and then simply listing them in the view based on that rank. But that leaves me with the same problem (not knowing how to do conditional sorting) it just does it before the save rather than when I read it out of the db.

Any help would be appreciated!

1 Answer 1

17

Use sort_by and supply an array of values. It will compare them in order from left to right. Default sort is ascending, so if you want the opposite (e.g. matches_won should sort descendingly so that most matches one comes first, as opposed to matches_lost, which should sort lowest to highest), you need to negate the value.

Here's an example

require 'pp'
Team = Struct.new :won, :lost, :demerits, :style

teams = Array.new(20) { Team.new rand(5), rand(5), rand(5), rand(5) }
puts "Before sort:"
pp teams

puts "", "After sort:"
pp teams
teams.sort_by! { |team| [-team.won, team.lost, team.demerits, -team.style] }
# >> Before sort:
# >> [#<struct Team won=1, lost=2, demerits=4, style=3>,
# >>  #<struct Team won=0, lost=4, demerits=2, style=2>,
# >>  #<struct Team won=4, lost=1, demerits=2, style=3>,
# >>  #<struct Team won=1, lost=4, demerits=1, style=4>,
# >>  #<struct Team won=3, lost=1, demerits=1, style=4>,
# >>  #<struct Team won=3, lost=4, demerits=1, style=2>,
# >>  #<struct Team won=4, lost=0, demerits=4, style=0>,
# >>  #<struct Team won=3, lost=4, demerits=1, style=2>,
# >>  #<struct Team won=1, lost=4, demerits=1, style=2>,
# >>  #<struct Team won=3, lost=0, demerits=1, style=1>,
# >>  #<struct Team won=3, lost=4, demerits=3, style=4>,
# >>  #<struct Team won=1, lost=4, demerits=0, style=0>,
# >>  #<struct Team won=3, lost=4, demerits=2, style=0>,
# >>  #<struct Team won=3, lost=0, demerits=3, style=0>,
# >>  #<struct Team won=1, lost=2, demerits=0, style=1>,
# >>  #<struct Team won=3, lost=0, demerits=1, style=0>,
# >>  #<struct Team won=0, lost=4, demerits=1, style=4>,
# >>  #<struct Team won=1, lost=4, demerits=3, style=0>,
# >>  #<struct Team won=3, lost=3, demerits=2, style=3>,
# >>  #<struct Team won=0, lost=4, demerits=4, style=4>]
# >> 
# >> After sort:
# >> [#<struct Team won=4, lost=0, demerits=4, style=0>,
# >>  #<struct Team won=4, lost=1, demerits=2, style=3>,
# >>  #<struct Team won=3, lost=0, demerits=1, style=1>,
# >>  #<struct Team won=3, lost=0, demerits=1, style=0>,
# >>  #<struct Team won=3, lost=0, demerits=3, style=0>,
# >>  #<struct Team won=3, lost=1, demerits=1, style=4>,
# >>  #<struct Team won=3, lost=3, demerits=2, style=3>,
# >>  #<struct Team won=3, lost=4, demerits=1, style=2>,
# >>  #<struct Team won=3, lost=4, demerits=1, style=2>,
# >>  #<struct Team won=3, lost=4, demerits=2, style=0>,
# >>  #<struct Team won=3, lost=4, demerits=3, style=4>,
# >>  #<struct Team won=1, lost=2, demerits=0, style=1>,
# >>  #<struct Team won=1, lost=2, demerits=4, style=3>,
# >>  #<struct Team won=1, lost=4, demerits=0, style=0>,
# >>  #<struct Team won=1, lost=4, demerits=1, style=4>,
# >>  #<struct Team won=1, lost=4, demerits=1, style=2>,
# >>  #<struct Team won=1, lost=4, demerits=3, style=0>,
# >>  #<struct Team won=0, lost=4, demerits=1, style=4>,
# >>  #<struct Team won=0, lost=4, demerits=2, style=2>,
# >>  #<struct Team won=0, lost=4, demerits=4, style=4>]
Sign up to request clarification or add additional context in comments.

2 Comments

That works great on 3 of the criteria I'm comparing but not the 4th. If there are two teams with the same matches_won I need to somehow check and see if they have played eachother and if so, who won and rank the winner in the higher spot. I'm sure this question is so specific to me that it is hard to answer. I will keep googling. Thanks for your help.
Use sort instead of sort_by. Then it will give you the two teams being compared, and you can compare by whatever you want. Check the docs for examples of how to do this. rubydoc.info/stdlib/core/1.9.3/Enumerable#sort-instance_method

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.