# Sorting keys of hash based on value

**URL:** <https://www.ruby-forum.com/t/sorting-keys-of-hash-based-on-value/185562>\
**Category:** Ruby\
**Created:** [March 22, 2010, 7:13pm UTC](https://www.ruby-forum.com/t/sorting-keys-of-hash-based-on-value/185562 "2010-03-22T19:13:24Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![trevoke](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ee7513/32.png) [@trevoke](https://www.ruby-forum.com/u/trevoke)\
**Post date:** [March 22, 2010, 7:13pm UTC](https://www.ruby-forum.com/t/sorting-keys-of-hash-based-on-value/185562/1 "2010-03-22T19:13:24Z")

</div>

Let’s say we have this contrived example:

hash = {:a =\> {:happy =\> 5},  
:b =\> {:happy =\> 4},  
:c =\> {:happy =\> 7}  
}

I would like to get the keys sorted by descending :happy value, like so:

[:c, :a, :b]

How would I do this?  
The best I’ve come up with is this:

irb(main):018:0\> hash.sort\_by { |x, y| -y[:happy] }  
=\> [[:c, {:happy=\>7}], [:a, {:happy=\>5}], [:b, {:happy=\>4}]]

It’s not elegant ☹

---

<div class="post-metadata">

**Author:** ![trevoke](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ee7513/32.png) [@trevoke](https://www.ruby-forum.com/u/trevoke)\
**Post date:** [March 22, 2010, 7:34pm UTC](https://www.ruby-forum.com/t/sorting-keys-of-hash-based-on-value/185562/2 "2010-03-22T19:34:52Z")

</div>

Aldric G. wrote:

> How would I do this?  
> The best I’ve come up with is this:
> 
> irb(main):018:0\> hash.sort\_by { |x, y| -y[:happy] }  
> =\> [[:c, {:happy=\>7}], [:a, {:happy=\>5}], [:b, {:happy=\>4}]]
> 
> It’s not elegant ☹

Do you want keys or the whole hash?

irb(main):015:0\> hash.keys.sort { |a,b| hash[b][:happy] \<=\>  
hash[a][:happy] }  
=\> [:c, :a, :b]

---

<div class="post-metadata">

**Author:** ![trevoke](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ee7513/32.png) [@trevoke](https://www.ruby-forum.com/u/trevoke)\
**Post date:** [March 22, 2010, 7:40pm UTC](https://www.ruby-forum.com/t/sorting-keys-of-hash-based-on-value/185562/3 "2010-03-22T19:40:58Z")

</div>

Riccardo Cecolin wrote:

> > 
> 
> Do you want keys or the whole hash?
> 
> irb(main):015:0\> hash.keys.sort { |a,b| hash[b][:happy] \<=\>  
> hash[a][:happy] }  
> =\> [:c, :a, :b]

Of course… I was trying to make it too complicated. Thanks!

---

<div class="post-metadata">

**Author:** ![trevoke](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ee7513/32.png) [@trevoke](https://www.ruby-forum.com/u/trevoke)\
**Post date:** [March 22, 2010, 9:40pm UTC](https://www.ruby-forum.com/t/sorting-keys-of-hash-based-on-value/185562/4 "2010-03-22T21:40:49Z")

</div>

On 03/22/2010 08:49 PM, Rob B. wrote:

> Or the slightly simpler sort\_by
> 
> hash.keys.sort\_by {|k| hast[k][:happy] }

Just for the variety:

hash.sort\_by {|k,v| -v[:happy]}.map(&:first)

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![trevoke](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ee7513/32.png) [@trevoke](https://www.ruby-forum.com/u/trevoke)\
**Post date:** [March 22, 2010, 8:50pm UTC](https://www.ruby-forum.com/t/sorting-keys-of-hash-based-on-value/185562/5 "2010-03-22T20:50:27Z")

</div>

On Mar 22, 2010, at 2:40 PM, Aldric G. wrote:

> Riccardo Cecolin wrote:
> 
> > > 
> > 
> > Do you want keys or the whole hash?
> > 
> > irb(main):015:0\> hash.keys.sort { |a,b| hash[b][:happy] \<=\>  
> > hash[a][:happy] }  
> > =\> [:c, :a, :b]
> 
> Of course… I was trying to make it too complicated. Thanks!

Or the slightly simpler sort\_by

hash.keys.sort\_by {|k| hast[k][:happy] }

If the hash.size is large, this can be a big performance win, but with  
just three keys, you won’t notice any difference.

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid
