Sorting the array

Hai

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
this,

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

Regards
Selvaraj

Use:
@attributes.sort_by{|a|a.updated_at}

selvaraj wrote:

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

Regards
Selvaraj

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.


Michael W.

Thank u very much,

Its working fine.

Michael W. wrote:

selvaraj wrote:

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

Regards
Selvaraj

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.


Michael W.