# Sort hashes using values

**URL:** <https://www.ruby-forum.com/t/sort-hashes-using-values/131494>\
**Category:** Ruby\
**Created:** [March 10, 2008, 5:37pm UTC](https://www.ruby-forum.com/t/sort-hashes-using-values/131494 "2008-03-10T17:37:00Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![subbu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a88e57/32.png) [@subbu](https://www.ruby-forum.com/u/subbu)\
**Post date:** [March 10, 2008, 5:37pm UTC](https://www.ruby-forum.com/t/sort-hashes-using-values/131494/1 "2008-03-10T17:37:00Z")

</div>

Ruby automatically sorts hashes by keys, which means:

> > h = {“first”=\>2,“second”=\>1,“third”=\>3}  
> > =\> {“third”=\>3, “second”=\>1, “first”=\>2}

How do I sort this by the values? So that I have:  
{“second”=\>1, “first”=\>2, “third”=\>3}

---

<div class="post-metadata">

**Author:** ![subbu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a88e57/32.png) [@subbu](https://www.ruby-forum.com/u/subbu)\
**Post date:** [March 10, 2008, 5:47pm UTC](https://www.ruby-forum.com/t/sort-hashes-using-values/131494/2 "2008-03-10T17:47:25Z")

</div>

Subbu wrote:

> Ruby automatically sorts hashes by keys, which means:
> 
> > > h = {“first”=\>2,“second”=\>1,“third”=\>3}  
> > > =\> {“third”=\>3, “second”=\>1, “first”=\>2}
> 
> How do I sort this by the values? So that I have:  
> {“second”=\>1, “first”=\>2, “third”=\>3}

-------------\> [class Hash - RDoc Documentation](http://www.ruby-doc.org/core/classes/Hash.html)  
hsh.sort =\> array  
hsh.sort {| a, b | block } =\> array

Converts hsh to a nested array of [key, value] arrays and sorts it,  
using Array#sort.

h = { “a” =\> 20, “b” =\> 30, “c” =\> 10 }  
h.sort #=\> [[“a”, 20], [“b”, 30], [“c”, 10]]  
h.sort {|a,b| a[1]\<=\>b[1]} #=\> [[“c”, 10], [“a”, 20], [“b”, 30]]

---

<div class="post-metadata">

**Author:** ![subbu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a88e57/32.png) [@subbu](https://www.ruby-forum.com/u/subbu)\
**Post date:** [March 10, 2008, 6:43pm UTC](https://www.ruby-forum.com/t/sort-hashes-using-values/131494/3 "2008-03-10T18:43:16Z")

</div>

2008/3/10, Rodrigo B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> -------------\> [class Hash - RDoc Documentation](http://www.ruby-doc.org/core/classes/Hash.html)  
> hsh.sort =\> array  
> hsh.sort {| a, b | block } =\> array
> 
> Converts hsh to a nested array of [key, value] arrays and sorts it,  
> using Array#sort.
> 
> h = { “a” =\> 20, “b” =\> 30, “c” =\> 10 }  
> h.sort #=\> [[“a”, 20], [“b”, 30], [“c”, 10]]  
> h.sort {|a,b| a[1]\<=\>b[1]} #=\> [[“c”, 10], [“a”, 20], [“b”, 30]]

and with #sort\_by

irb(main):001:0\> {“first”=\>2,“second”=\>1,“third”=\>3}.sort\_by {|k,v| v}  
=\> [[“second”, 1], [“first”, 2], [“third”, 3]]

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![subbu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a88e57/32.png) [@subbu](https://www.ruby-forum.com/u/subbu)\
**Post date:** [March 10, 2008, 6:49pm UTC](https://www.ruby-forum.com/t/sort-hashes-using-values/131494/4 "2008-03-10T18:49:45Z")

</div>

On Mon, Mar 10, 2008 at 11:34 AM, Subbu [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Ruby automatically sorts hashes by keys, which means:
> 
> > > h = {“first”=\>2,“second”=\>1,“third”=\>3}  
> > > =\> {“third”=\>3, “second”=\>1, “first”=\>2}
> 
> How do I sort this by the values? So that I have:  
> {“second”=\>1, “first”=\>2, “third”=\>3}

A hash, by nature, is not really sorted IIRC. If you sort, you need  
an Array object as a return value, which means using #sort\_by…

h = Hash[“first”, 2, “second”, 1, “third”, 3]  
h.sort\_by {|k, v| v}

It will give you an array of arrays.

Todd

---

<div class="post-metadata">

**Author:** ![subbu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a88e57/32.png) [@subbu](https://www.ruby-forum.com/u/subbu)\
**Post date:** [March 11, 2008, 7:13am UTC](https://www.ruby-forum.com/t/sort-hashes-using-values/131494/5 "2008-03-11T07:13:25Z")

</div>

On Mar 10, 10:48 am, Todd B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> an Array object as a return value, which means using #sort\_by…
> 
> h = Hash[“first”, 2, “second”, 1, “third”, 3]  
> h.sort\_by {|k, v| v}
> 
> It will give you an array of arrays.
> 
> Todd

Thanks so much guys.
