[英]how to prevent stacked ordering when I do Z=X+Y? X is always on top
Here's what I mean 这就是我的意思
This is my controller 这是我的控制器
class StaticPagesController < ApplicationController
def home
if signed_in?
@post_items = current_user.posts
@activities = PublicActivity::Activity.order("created_at desc")
@items = @post_items + @activities
@items.sort_by{|item| item.class == PublicActivity::Activity ? item.created_at : item.created_at}
@items = @items.paginate(:page => 1, :per_page => 20)
else
redirect_to root_path
end
end
As you can see in the above, this line @items = @post_items + @activities
causes stacked ordering. 正如您在上面看到的那样,此行
@items = @post_items + @activities
导致堆叠排序。 All of the post_items are ordered first THAN the activities are ordered below it. 所有的post_items都比其下的活动先被排序。
I am trying to combine BOTH into one ordering using "created_at". 我正在尝试使用“ created_at”将两者合并为一个顺序。
How can I prevent this stacked ordering and make it order as one? 如何防止这种堆叠式订购并将其作为一体订购? Thanks
谢谢
sort_by
isn't doing what you think it's doing. sort_by
并未按照您认为的去做。 It's returning a new sorted array. 它返回一个新的排序数组。 If you want to modify the array in-place, you have to use
sort_by!
如果要就地修改数组,则必须使用
sort_by!
: :
@items.sort_by!(&:created_at)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.