How to sort list of object?

Hi, I want sort list of object by field "num". I try @blocks = Block.find_all @blocks.collect{|block| block.attributes.sort_by{|item| item["num"]}} but I get TypeError: can't convert String into Integer   from (irb):37:in `'   from (irb):37   from ./script/../config/../config/../vendor/rails/activerecord/lib/active_record/base.rb:2068:in `sort_by'   from (irb):37:in `each'   from (irb):37:in `sort_by'   from (irb):37   from (irb):37:in `collect'   from (irb):37

How to do it correctly?

You could do: @blocks = Block.find(:all) @blocks.sort! {|a,b| a.num<=> b.num}

thanks

Hi --

Hi, I want sort list of object by field "num". I try @blocks = Block.find_all

find_all is deprecated in favor of find(:all). (find_all is still an OK method for enumerable classes in Ruby, like Array and Hash, but the ActiveRecord version is deprecated.)

@blocks.collect{|block| block.attributes.sort_by{|item| item["num"]}} but I get TypeError: can't convert String into Integer

A hybrid AR/Ruby version would look like this:

   @blocks = Block.find(:all).sort_by {|b| b.num }

but you can also ask the database to do the sorting work:

   @blocks = Block.find(:all, :order => "num")

David