ruby on rails - Problem sorting an array -
i'm mixing 2 arrays , want sort them created_at attribute:
@current_user_statuses = current_user.statuses @friends_statuses = current_user.friends.collect { |f| f.statuses } @statuses = @current_user_statuses + @friends_statuses @statuses.flatten!.sort!{ |a,b| b.created_at <=> a.created_at } the @current_user_statuses , @friends_statuses each sort correctly, combined sort incorrectly, @friends_statuses showing on top sorted created_at attribute , @current_user_statuses on bottom sorted created_at attribute.
this view:
<% @statuses.each |d| %> <%= d.content %> <% end %>
try:
(current_user.statuses + current_user.friends.collect(&:statuses)) \ .flatten.compact.sort_by(&:created_at)
Comments
Post a Comment