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

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -