Skip to content

Instantly share code, notes, and snippets.

@michaellouieloria
Created February 26, 2015 06:22
Show Gist options
  • Save michaellouieloria/c8536cb3171c0396152f to your computer and use it in GitHub Desktop.
Save michaellouieloria/c8536cb3171c0396152f to your computer and use it in GitHub Desktop.
Sort merged activerecord objects in Rails
@posts = @posts1 + @posts2
#Descending
@posts.sort! {|x,y| y.created_at <=> x.created_at}
#Ascending
@posts.sort! {|x,y| x.created_at <=> y.created_at}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment