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

**URL:** <https://www.ruby-forum.com/t/sort-array-of-hashes-by-a-hash-value-and-internally-sort-array-that-is-another-hash-value/261620>\
**Category:** Uncategorized\
**Created:** [July 29, 2021, 10:24pm UTC](https://www.ruby-forum.com/t/sort-array-of-hashes-by-a-hash-value-and-internally-sort-array-that-is-another-hash-value/261620 "2021-07-29T22:24:16Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![thelmalu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c6cbf5/32.png) [@thelmalu](https://www.ruby-forum.com/u/thelmalu)\
**Post date:** [July 29, 2021, 10:24pm UTC](https://www.ruby-forum.com/t/sort-array-of-hashes-by-a-hash-value-and-internally-sort-array-that-is-another-hash-value/261620/1 "2021-07-29T22:24:16Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![thelmalu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c6cbf5/32.png) [@thelmalu](https://www.ruby-forum.com/u/thelmalu)\
**Post date:** [July 30, 2021, 3:55am UTC](https://www.ruby-forum.com/t/sort-array-of-hashes-by-a-hash-value-and-internally-sort-array-that-is-another-hash-value/261620/2 "2021-07-30T03:55:21Z")

</div>

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.
