Sort array of hashes by a hash value AND internally sort array that is another hash value

I have an array of hashes, say aray.
I want to sort aray so that the :a hash values are in ascending order and the :c hash value arrays are sorted in ascending order

aray = [{a: 7, b: 22, c:[12,4,-1]}, {a: -3, b: 2, c:[1,-1,-1]}, {a: 30, b: 9, c:[2,4,8]}]
goes to
s_aray = [{a: -3, b: 2, c:[-1,-1,1]}, {a: 7, b: 22, c:[-1,4,12]}, {a: 30, {b: 9, c:[2,4,8]}]

How do I do this?

I’ve found something that works:

aray.sort_by { |elem| elem[:a] }.each { |e| e[:c] = (e[:c]).sort }

I had gotten pretty close, but I was using map instead of each, and so losing some of the hash key/value pairs.

1 Like

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs