Forum: Ruby on Rails Sort an Array of ActiveRecords objects by created_at

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Bensoussan M. (Guest)
on 2008-10-24 18:42
(Received via mailing list)
Hi,
I have an array of activerecords objects. I'd like to sort it  by
created_at column (they all have one).
Do you see a smart way to do it ??

Thanks,

Mike
Thorsten M. (Guest)
on 2008-10-24 18:55
(Received via mailing list)
what's wrong with:

@my_model = Model.find(:all, :order => "created_at ASC"))
Bensoussan M. (Guest)
on 2008-10-24 18:59
(Received via mailing list)
My array is composed by different activerecord objects:

my_model1 = Model.find(:all)

my_model2 = Model.find(:all)

@my_models = my_model1 + mymodel2

and now i'd like to sort @my_models by "created_at"
Craig D. (Guest)
on 2008-10-24 19:10
(Received via mailing list)
On Fri, Oct 24, 2008 at 10:59 AM, Bensoussan M.
<removed_email_address@domain.invalid>wrote:

>
> My array is composed by different activerecord objects:
>
> my_model1 = Model.find(:all)
>
> my_model2 = Model.find(:all)
>
> @my_models = my_model1 + mymodel2
>
> and now i'd like to sort @my_models by "created_at"


@my_models = @my_models.sort_by { |m| m.created_at }

Note that Enumerable#sort_by returns a new array.

Regards,
Craig
Bensoussan M. (Guest)
on 2008-10-24 19:13
(Received via mailing list)
Yes  that's it ! !

Thank you,
Mike.

On Oct 24, 5:10 pm, "Craig D." <removed_email_address@domain.invalid>
Maurício L. (Guest)
on 2008-10-24 19:16
(Received via mailing list)
Even simpler:

my_model1 = Model.find(:all)
my_model2 = Model.find(:all)

@my_models = (my_model1 + mymodel2).sort_by( &:created_at )

On Fri, Oct 24, 2008 at 12:12 PM, Bensoussan M. 
<removed_email_address@domain.invalid>
wrote:
>
> Yes  that's it ! !
>
> Thank you,
> Mike.


--
Maurício Linhares
http://alinhavado.wordpress.com/ (pt-br) | http://blog.codevader.com/
(en)
João Pessoa, PB, +55 83 8867-7208
Bensoussan M. (Guest)
on 2008-10-24 19:19
(Received via mailing list)
Thanks Maurício,

On Oct 24, 5:15 pm, "Maurício Linhares" <removed_email_address@domain.invalid>
wrote:
> Even simpler:
>
> my_model1 = Model.find(:all)
> my_model2 = Model.find(:all)
>
> @my_models = (my_model1 + mymodel2).sort_by( &:created_at )
>

What the '&' do precisely ?
Maurício L. (Guest)
on 2008-10-24 19:32
(Received via mailing list)
Hi Michael,

The &:created_at is just a shorthand for &Proc.new { |i| i.created_at }

On Fri, Oct 24, 2008 at 12:18 PM, Bensoussan M. 
<removed_email_address@domain.invalid>
wrote:
>> @my_models = (my_model1 + mymodel2).sort_by( &:created_at )
>>
>> --
>> Maurício Linhareshttp://alinhavado.wordpress.com/(pt-br) 
|http://blog.codevader.com/(en)
>> João Pessoa, PB, +55 83 8867-7208
> >
>



--
Maurício Linhares
http://alinhavado.wordpress.com/ (pt-br) | http://blog.codevader.com/
(en)
João Pessoa, PB, +55 83 8867-7208
Bensoussan M. (Guest)
on 2008-10-24 19:35
(Received via mailing list)
excellent ! Thanks for the tip.

Mike

On Oct 24, 5:31 pm, "Maurício Linhares" <removed_email_address@domain.invalid>
Bensoussan M. (Guest)
on 2008-10-24 20:02
(Received via mailing list)
excellent ! Thanks for the tip.

Mike

On Oct 24, 5:31 pm, "Maurício Linhares" <removed_email_address@domain.invalid>
Frederick C. (Guest)
on 2008-10-24 22:54
(Received via mailing list)
On Oct 24, 4:31 pm, "Maurício Linhares" <removed_email_address@domain.invalid>
wrote:
> Hi Michael,
>
> The &:created_at is just a shorthand for &Proc.new { |i| i.created_at }
>
Although it should be noted that this (also known as Symbol#to_proc)
is a lot slower than just writing the equivalent block (but not in
ruby 1.9 or if my memory is correct, 1.8.7)

Fred
This topic is locked and can not be replied to.