我有一个对象数组,我正在尝试按多个条件排序。大多数比较只是在哈希上进行<=>
,因此使用sort_by
非常快,但其中一个更复杂。
该阵列属于足球队,目前正按照以下方式排序:
teams.sort_by { |item| [item.points, item.goal_dif, item.goals] }
但是,如果最后2个团队在这3个字段中具有相同的值,我希望决胜局是我所做的功能,a_beat_b(teamA, teamB)
。
我尝试使用Array.sort
,但与前{...}}相比,sort_by
非常慢...我的实现是这样的:
teams.sort ( |a,b| [a.points, a.goals_dif, a.goals] <=> [b.points, b.goals_dif, b.goals])
与sort_by相比,它非常慢。 points,goals_dif和目标的函数需要一些简单的查询,但如果它必须做数百个就会陷入困境。
我不擅长Ruby,所以不确定将a_beats_b
放在哪里。 (如果A击败,则返回1,0或-1,对B进行吸引或丢失),
答案 0 :(得分:3)
我尝试使用
非常慢Array.sort
,但与sort_by
相比,前几个
这是因为sort
多次调用给定的块。这是一个示例,展示了幕后发生的事情:(按长度排序"apple"
,"pear"
和"fig"
)
def length(str)
puts "calculating #{str.inspect}.length"
str.length
end
array = %w{apple pear fig}
array.sort { |a, b| length(a) <=> length(b) }
#=> ["fig", "pear", "apple"]
我们length
方法的输出:
calculating "apple".length
calculating "pear".length
calculating "apple".length
calculating "fig".length
calculating "pear".length
calculating "fig".length
如您所见,在排序期间多次调用length
。想象一下,这些都是数据库查询。
sort_by
为每个元素调用一次块,构建一个内部映射:
array.sort_by { |a| length(a) }
#=> ["fig", "pear", "apple"]
输出:
calculating "apple".length
calculating "pear".length
calculating "fig".length
对于昂贵的操作(如数据库查询),这要快得多。但它的灵活性也较低 - 您无法再动态比较a
和b
。
然而,您可以存储(昂贵的)操作的结果,例如使用哈希:(这称为memoization)
hash = Hash.new { |h, k| h[k] = length(k) }
并使用sort
中的哈希:
array.sort { |a, b| hash[a] <=> hash[b] }
# calculating "apple".length
# calculating "pear".length
# calculating "fig".length
#=> ["fig", "pear", "apple"]
排序后,我们的哈希看起来像这样:
hash #=> {"apple"=>5, "pear"=>4, "fig"=>3}
应用于您的代码,这样的事情应该有效:
hash = Hash.new { |h, k| h[k] = [k.points, k.goal_dif, k.goals] }
teams.sort { |a, b| hash[a] == hash[b] ? a_beats_b(a, b) : hash[a] <=> hash[b] }
答案 1 :(得分:2)
包含sort
的{{1}}的实施:
a_beats_b
答案 2 :(得分:2)
这是另一种方法,尽管有点复杂,但它的设计是为了提高效率。该方法使用以下步骤。
Team
实例转换为包含实例的数组和要在其上进行廉价排序的三元素数组。Team
实例。 Team
个实例组后,按a_beat_b(a, b)
方法对其进行排序(当然,除非该组只包含一个团队)。 <强>代码强>
def sort_em(teams)
teams.map { |t| [t, [t.points, t.goal_dif, t.goals]] }.
sort_by(&:last).
chunk(&:last).
map { |_,tied_teams| tied_teams.map(&:first) }.
flat_map { |tied_teams| (tied_teams.size == 1) ?
tied_teams.first : tied_teams.sort { |a,b| a_beat_b(a, b) } }
end
示例强>
class Team
attr_reader :name, :points, :goal_dif, :goals
def initialize(name, points, goal_dif, goals)
@name, @points, @goal_dif, @goals = name, points, goal_dif, goals
end
end
teams = [Team.new("bluebirds", 233, 25, 130),
Team.new("eagles", 233, 18, 105),
Team.new("jays", 233, 25, 130),
Team.new("owls", 160, 12, 105),
Team.new("sparrows", 233, 18, 105)
]
#=> [#<Team:0x007ff2f900e5a8 @name="bluebirds", @points=233, @goal_dif=25, @goals=130>,
# #<Team:0x007ff2f900e530 @name="eagles", @points=233, @goal_dif=18, @goals=105>,
# #<Team:0x007ff2f900e4b8 @name="jays", @points=233, @goal_dif=25, @goals=130>,
# #<Team:0x007ff2f900e440 @name="owls", @points=160, @goal_dif=12, @goals=105>,
# #<Team:0x007ff2f900e3c8 @name="sparrows", @points=233, @goal_dif=18, @goals=105>]
def a_beat_b(a, b)
a.name.size <=> b.name.size
end
sort_em(teams)
#=> [#<Team:0x007ff2fa845630 @name="owls", @points=160, @goal_dif=12, @goals=105>,
# #<Team:0x007ff2fa845720 @name="eagles", @points=233, @goal_dif=18, @goals=105>,
# #<Team:0x007ff2fa8455b8 @name="sparrows", @points=233, @goal_dif=18, @goals=105>,
# #<Team:0x007ff2fa8456a8 @name="jays", @points=233, @goal_dif=25, @goals=130>,
# #<Team:0x007ff2fa8457e8 @name="bluebirds", @points=233, @goal_dif=25, @goals=130>]
<强>解释强>
步骤如下。
a = teams.map { |t| [t, [t.points, t.goal_dif, t.goals]] }
#=> [[#<Team:0x007ff2fa8457e8 @name="bluebirds", @points=233, @goal_dif=25, @goals=130>,
# [233, 25, 130]],
# [#<Team:0x007ff2fa845720 @name="eagles", @points=233, @goal_dif=18, @goals=105>,
# [233, 18, 105]],
# [#<Team:0x007ff2fa8456a8 @name="jays", @points=233, @goal_dif=25, @goals=130>,
# [233, 25, 130]],
# [#<Team:0x007ff2fa845630 @name="owls", @points=160, @goal_dif=12, @goals=105>,
# [160, 12, 105]],
# [#<Team:0x007ff2fa8455b8 @name="sparrows", @points=233, @goal_dif=18, @goals=105>,
# [233, 18, 105]]]
b = a.sort_by(&:last)
#=> [[#<Team:0x007ff2fa845630 @name="owls", @points=160, @goal_dif=12, @goals=105>,
# [160, 12, 105]],
# [#<Team:0x007ff2fa845720 @name="eagles", @points=233, @goal_dif=18, @goals=105>,
# [233, 18, 105]],
# [#<Team:0x007ff2fa8455b8 @name="sparrows", @points=233, @goal_dif=18, @goals=105>,
# [233, 18, 105]],
# [#<Team:0x007ff2fa8457e8 @name="bluebirds", @points=233, @goal_dif=25, @goals=130>,
# [233, 25, 130]],
# [#<Team:0x007ff2fa8456a8 @name="jays", @points=233, @goal_dif=25, @goals=130>,
# [233, 25, 130]]
# ]
c = b.chunk(&:last)
#=> #<Enumerator: #<Enumerator::Generator:0x007ff2fa81dc20>:each>
要查看枚举器c
生成的值,我们可以将其转换为数组。
c.to_a
#=> [[[160, 12, 105],
# [[#<Team:0x007ff2fa845630 @name="owls",@points=160,@goal_dif=12,@goals=105>,
# [160, 12, 105]
# ]
# ]
# ],
# [[233, 18, 105],
# [[#<Team:0x007ff2fa845720 @name="eagles",@points=233,@goal_dif=18,@goals=105>,
# [233, 18, 105]
# ],
# [#<Team:0x007ff2fa8455b8 @name="sparrows",@points=233,@goal_dif=18,@goals=105>,
# [233, 18, 105]
# ]
# ],
# [[233, 25, 130],
# [[#<Team:0x007ff2fa8457e8 @name="bluebirds",@points=233,@goal_dif=25,@goals=130>,
# [233, 25, 130]
# ],
# [#<Team:0x007ff2fa8456a8 @name="jays", @points=233,@goal_dif=25,@goals=130>,
# [233, 25, 130]
# ]
# ]
# ]
# ]
d = c.map { |_,tied_teams| tied_teams.map(&:first) }
#=> [[#<Team:0x007ff2fa845630 @name="owls", @points=160, @goal_dif=12, @goals=105>],
# [#<Team:0x007ff2fa845720 @name="eagles", @points=233, @goal_dif=18, @goals=105>,
# #<Team:0x007ff2fa8455b8 @name="sparrows", @points=233, @goal_dif=18, @goals=105>
# ],
# [#<Team:0x007ff2fa8457e8 @name="bluebirds", @points=233, @goal_dif=25, @goals=130>,
# #<Team:0x007ff2fa8456a8 @name="jays", @points=233, @goal_dif=25, @goals=130>
# ]
# ]
d.flat_map { |tied_teams| (tied_teams.size == 1) ?
tied_teams.first : tied_teams.sort { |a,b| a_beat_b(a, b) } }
#=> [#<Team:0x007ff2fa845630 @name="owls", @points=160, @goal_dif=12, @goals=105>,
# #<Team:0x007ff2fa845720 @name="eagles", @points=233, @goal_dif=18, @goals=105>,
# #<Team:0x007ff2fa8455b8 @name="sparrows", @points=233, @goal_dif=18, @goals=105>,
# #<Team:0x007ff2fa8456a8 @name="jays", @points=233, @goal_dif=25, @goals=130>,
# #<Team:0x007ff2fa8457e8 @name="bluebirds", @points=233, @goal_dif=25, @goals=130>
# ]
答案 3 :(得分:0)
根据排序函数的大小和常数,这可能是一种方法:
# First group the teams by standard sort:
groups = teams.group_by{|a| [a.points, a.goals_dif, a.goals] }
# For each group that has ties. Run the slow sorter on them:
groups.each{ |_,val| val.sort!{|teamA,teamB| a_beat_b(teamA, teamB)} if val.size > 1 }
# Finally run sort on the keys of the group by:
groups.sort.flat_map(&:last)