Sort_by not working

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?

On 10 Dec 2008, at 21:33, SurviveStyle5 wrote:

#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

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 C. [email protected]

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs