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.
Bd54294eb31aa4082836b46cd7144d5a?d=identicon&s=25 Bensoussan Michael (Guest)
on 2008-10-24 16: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
2d8132658d56e51f19ace1c68e48b6aa?d=identicon&s=25 Thorsten Mueller (thorsten)
on 2008-10-24 16:55
(Received via mailing list)
what's wrong with:

@my_model = Model.find(:all, :order => "created_at ASC"))
Bd54294eb31aa4082836b46cd7144d5a?d=identicon&s=25 Bensoussan Michael (Guest)
on 2008-10-24 16: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"
B14575f0ca69f10938fdd67e7156e0e1?d=identicon&s=25 Craig Demyanovich (Guest)
on 2008-10-24 17:10
(Received via mailing list)
On Fri, Oct 24, 2008 at 10:59 AM, Bensoussan Michael
<pada51@gmail.com>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
Bd54294eb31aa4082836b46cd7144d5a?d=identicon&s=25 Bensoussan Michael (Guest)
on 2008-10-24 17:13
(Received via mailing list)
Yes  that's it ! !

Thank you,
Mike.

On Oct 24, 5:10 pm, "Craig Demyanovich" <cdemyanov...@gmail.com>
A91bd6cef23eb3516245a092e196c4da?d=identicon&s=25 Maurício Linhares (mauricio)
on 2008-10-24 17: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 Michael <pada51@gmail.com>
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
Bd54294eb31aa4082836b46cd7144d5a?d=identicon&s=25 Bensoussan Michael (Guest)
on 2008-10-24 17:19
(Received via mailing list)
Thanks Maurício,

On Oct 24, 5:15 pm, "Maurício Linhares" <mauricio.linha...@gmail.com>
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 ?
A91bd6cef23eb3516245a092e196c4da?d=identicon&s=25 Maurício Linhares (mauricio)
on 2008-10-24 17: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 Michael <pada51@gmail.com>
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
Bd54294eb31aa4082836b46cd7144d5a?d=identicon&s=25 Bensoussan Michael (Guest)
on 2008-10-24 17:35
(Received via mailing list)
excellent ! Thanks for the tip.

Mike

On Oct 24, 5:31 pm, "Maurício Linhares" <mauricio.linha...@gmail.com>
Bd54294eb31aa4082836b46cd7144d5a?d=identicon&s=25 Bensoussan Michael (Guest)
on 2008-10-24 18:02
(Received via mailing list)
excellent ! Thanks for the tip.

Mike

On Oct 24, 5:31 pm, "Maurício Linhares" <mauricio.linha...@gmail.com>
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2008-10-24 20:54
(Received via mailing list)
On Oct 24, 4:31 pm, "Maurício Linhares" <mauricio.linha...@gmail.com>
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.