在我的项目中,我想从我的数据库中选择记录,然后将它们按与特定时间范围内的最新记录相似的时间发生的记录组合在一起。
例如,时间范围为 1 小时。如果用户在下午 4:30 到下午 5:15 之间创建了 3 个帖子,在下午 1:15 到下午 1:30 之间创建了 2 个帖子,在上午 10:00 发布了 1 个帖子,我希望创建如下结构:
user.posts.find(:all).group_by do |post|
# (posts have a created_at column containing both a date and time)
# Algorithm here
end
结果:
[
[Tue March 31 5:15pm, [post6,post5,post4]]
[Tue March 31 1:30pm, [post3,post2]]
[Tue March 31 10:00am, [post1]]
]
对执行此操作的最佳算法有任何想法吗?如果您不了解 Ruby,伪代码也可以。
编辑:谢谢乔尔。这是我最终使用的代码(提要而不是帖子):
def aggregate(feeds, timeLimit)
return [] if feeds.blank?
result = []
bin = []
feeds = feeds.sort_by { |f| -f.created_at.to_i }
bin_time = feeds.first.created_at
feeds.each do |feed|
if (bin_time - feed.created_at) < timeLimit
bin << feed
else
result << [bin_time, bin]
bin_time = feed.created_at
bin = [feed]
end
end
result << [bin_time, bin]
result
end