yoyo
yoyo

Reputation: 13

I want to sort the hash in the array

a = [{"name"=> "ted","age"=>1}, 
     {"name"=> "mika","age"=>2},
     {"name"=> "bob","age"=>0}]

How do I can sort the a is an array in age order (ascending order) with ruby program?

I would like to be the following.

a = [{"name"=> "bob","age"=>0},
     {"name"=> "ted","age"=>1},
     {"name"=> "mika","age"=>2}]

Upvotes: 0

Views: 53

Answers (1)

yoyo
yoyo

Reputation: 13

As Cary Swoveland told in comment,

a.sort_by { |h| h["age"] }

I could do as above.thanks.

Upvotes: 1

Related Questions