Sorting the array

selvaraj wrote:


    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

  @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?


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.