使用Ruby on Rails的多个条件对列表进行排序

时间:2012-01-10 01:25:52

标签: ruby-on-rails ruby

我希望能够根据4个不同的标准对列表中的12支队伍进行排名。

我将有一个名为Teams的数据库表,每个团队将有各种列,如matches_won,matches_lost等...

我想基于matches_won按顺序显示它们。这很简单。但是,如果两支球队在match_won中并列,那么我必须检查他们是否已经相互比赛以及谁赢了。如果他们有相同的match_won并且没有互相玩耍,他们的记录会在另外两个标准上进行比较并按其排名。我想如果我能弄清楚如何做一个额外的条件,我将能够弄清楚如何做其他人。所以为了简洁起见,我不会在这里详述它们。

我不知道如何在Rails中进行这种多级排序。

我原本以为可能在db表和before_save函数中添加一个'rank'列,它们将它们排入数据库,然后根据该等级在视图中列出它们。但这让我遇到了同样的问题(不知道如何进行条件排序)它只是在保存之前做的,而不是在我从数据库中读出它时。

任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:15)

使用sort_by并提供一系列值。它将按从左到右的顺序进行比较。默认排序是升序,所以如果你想要相反的(例如,matches_won应该降序排序,以便大多数匹配首先出现,而不是匹配最低到高等匹配的matches_lost),你需要否定该值。

这是一个例子

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>]