Hai I am facing the problem while try to sort. For example i have one object which is the result of query. That contains two field message and updated_at.Same way i ahve another object with the same field. Now i have merged the two object. After merging i am getting like this, @attributes={"message"=>"Hai this is first good message", "updated_at"=>"2007-03-26 11:14:02"}>, @attributes={"message"=>"", "updated_at"=>"2007-03-26 14:14:51", }>, Now I want to sort the result according to the updated_at field? Can any one help me? Regards Selvaraj -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
selvaraj wrote:> Hai > > I am facing the problem while try to sort. For example i have one > object which is the result of query. That contains two field message and > updated_at.Same way i ahve another object with the same field. > > Now i have merged the two object. After merging i am getting like > this, > > @attributes={"message"=>"Hai this is first good message", > "updated_at"=>"2007-03-26 11:14:02"}>, @attributes={"message"=>"", > "updated_at"=>"2007-03-26 14:14:51", }>, > > > Now I want to sort the result according to the updated_at field? > > Can any one help me? > > Regards > Selvaraj >If you''ve merged things into an array you can do something like: sorted_array = myarray.sort { |a,b| a["updated_at"] <=> b["updated_at"] } If you want the earliest hash at the beginning of the array. Otherwise you can swap a and b around in the comparison. -- Michael Wang --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Use: @attributes.sort_by{|a|a.updated_at} On Mar 26, 6:25 am, selvaraj <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Hai > > I am facing the problem while try to sort. For example i have one > object which is the result of query. That contains two field message and > updated_at.Same way i ahve another object with the same field. > > Now i have merged the two object. After merging i am getting like > this, > > @attributes={"message"=>"Hai this is first good message", > "updated_at"=>"2007-03-26 11:14:02"}>, @attributes={"message"=>"", > "updated_at"=>"2007-03-26 14:14:51", }>, > > Now I want to sort the result according to the updated_at field? > > Can any one help me? > > Regards > Selvaraj > > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Thank u very much, Its working fine. Michael Wang wrote:> selvaraj wrote: >> "updated_at"=>"2007-03-26 11:14:02"}>, @attributes={"message"=>"", >> "updated_at"=>"2007-03-26 14:14:51", }>, >> >> >> Now I want to sort the result according to the updated_at field? >> >> Can any one help me? >> >> Regards >> Selvaraj >> > > If you''ve merged things into an array you can do something like: > > sorted_array = myarray.sort { |a,b| a["updated_at"] <=> b["updated_at"] > } > > If you want the earliest hash at the beginning of the array. Otherwise > you can swap a and b around in the comparison. > > > -- > Michael Wang-- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---