I'm mixing 2 arrays and want to sort them by their 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
and @friends_statuses
each sort correctly, but combined they sort incorrectly, with the @friends_statuses
always showing up on top sorted by their created_at
attribute and the @current_user_statuses
on the bottom sorted by their created_at
attribute.
This is the view:
<% @statuses.each do |d| %>
<%= d.content %>
<% end %>