hi I have an array of topics and posts each has an updated_on field. I want to sort the array so in the order of the updated_on field. I.e 1 Topic updated on 5 march 2 Poll updated on 4 march 3 Topic updated on 6 march 4 poll updated on 7 march shoud go 4,3,1,2 my code looks like this @topicsAndPolls.sort { |a,b| a.updated_on <=> b.updated_on} but this isnt working. Do I have to define the <=> method somewhere. Can I pass that in the block aswell? Thanks Iain Adams --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
On Fri, 14 Sep 2007, Iain Adams wrote:> I have an array of topics and posts each has an updated_on field. I > want to sort the array so in the order of the updated_on field.[...]> my code looks like this > @topicsAndPolls.sort { |a,b| a.updated_on <=> b.updated_on} > [\n inserted] but this > isnt working. Do I have to define the <=> method somewhere. Can I passWhat do you assign the result of this to? Or did you mean @topicsAndPolls.sort! { |a,b| a.updated_on <=> b.updated_on} ? That''s s/sort/sort!/ in sed parlance, in case the addition of one char is as hard for you to spot as for me.> that in the block aswell?If updated_on returns a Date then <=> should work. If it doesn''t you''ll get a NoMethodError. Hugh --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
> I have an array of topics and posts each has an updated_on field. I > want to sort the array so in the order of the updated_on field. > > I.e > > 1 Topic updated on 5 march > 2 Poll updated on 4 march > 3 Topic updated on 6 march > 4 poll updated on 7 march > > shoud go 4,3,1,2 > > my code looks like this > @topicsAndPolls.sort { |a,b| a.updated_on <=> b.updated_on} but this > isnt working. Do I have to define the <=> method somewhere. Can I pass > that in the block aswell?In addition to the other reply, you''re looking for a descendant sort... so you really want: { |a,b| b.updated_on <=> a.updated_on} (not the switching of a and b) -philip --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---