I''m having a weird issue where if say.... someobject.items.sort_by {|item| item.key } #or someobject.items.sort_by &:key #then someobject.items.each do |item| #somestuff end #somestuff is not in sorted order. if i use different syntax... questionnaire.items.sort! { |a,b| a.key <=> b.key } this works fine. why is that? does it matter? rails --version Rails 2.1.1 ruby --version ruby 1.8.6 (2008-03-03 patchlevel 114) [universal-darwin9.0] thoughts? --~--~---------~--~----~------------~-------~--~----~ 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 10 Dec 2008, at 21:33, SurviveStyle5 wrote:> > I''m having a weird issue where if say.... > > someobject.items.sort_by {|item| item.key } > #or > someobject.items.sort_by &:key > #then > someobject.items.each do |item| > #somestuff > end > > #somestuff is not in sorted order. if i use different syntax... > > questionnaire.items.sort! { |a,b| a.key <=> b.key } > this works fine. why is that? does it matter? >sort_by does not change the array (it returns a new, sorted, array), as does sort. sort! on the other hand changes the array you call it on (as the ! indicates) Fred> rails --version > Rails 2.1.1 > > ruby --version > ruby 1.8.6 (2008-03-03 patchlevel 114) [universal-darwin9.0] > > > thoughts? > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Thanks! ok i couldn''t find sufficient documentation to tell me either way, thanks! it''s always something silly. On Dec 10, 3:50 pm, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 10 Dec 2008, at 21:33, SurviveStyle5 wrote: > > > > > > > I''m having a weird issue where if say.... > > > someobject.items.sort_by {|item| item.key } > > #or > > someobject.items.sort_by &:key > > #then > > someobject.items.each do |item| > > #somestuff > > end > > > #somestuff is not in sorted order. if i use different syntax... > > > questionnaire.items.sort! { |a,b| a.key <=> b.key } > > this works fine. why is that? does it matter? > > sort_by does not change the array (it returns a new, sorted, array), > as does sort. > sort! on the other hand changes the array you call it on (as the ! > indicates) > > Fred > > > rails --version > > Rails 2.1.1 > > > ruby --version > > ruby 1.8.6 (2008-03-03 patchlevel 114) [universal-darwin9.0] > > > thoughts?--~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---