samedi 27 juin 2015

Sort two array of hashes by the same criteria on Ruby

I am working on Ruby with two arrays of hashes like these:

a = [{'name'=> 'Ana', 'age'=> 42 },
     {'name'=> 'Oscar', 'age'=> 22 },
     {'name'=> 'Dany', 'age'=> 12 }]

b = [{'name'=> 'Dany', 'country'=> 'Canada' },
     {'name'=> 'Oscar', 'country'=> 'Peru'},
     {'name'=> 'Ana', 'country'=>'France'}]

I am sorting them like this:

a.sort_by!{|c| c['name']}
b.sort_by!{|c| c['name']}

and it works, but since I doing the same on both arrays, I would like doing the same but in one line; I mean, sort the two arrays at once.

How can I do it?

Aucun commentaire:

Enregistrer un commentaire