# Sorting a hash based on a attribute of teh objects which it

**URL:** <https://www.ruby-forum.com/t/sorting-a-hash-based-on-a-attribute-of-teh-objects-which-it/88647>\
**Category:** Ruby\
**Created:** [March 13, 2007, 10:45am UTC](https://www.ruby-forum.com/t/sorting-a-hash-based-on-a-attribute-of-teh-objects-which-it/88647 "2007-03-13T10:45:37Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![senthil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c89c15/32.png) [@senthil](https://www.ruby-forum.com/u/senthil)\
**Post date:** [March 13, 2007, 10:45am UTC](https://www.ruby-forum.com/t/sorting-a-hash-based-on-a-attribute-of-teh-objects-which-it/88647/1 "2007-03-13T10:45:37Z")

</div>

HI all,  
I want to sort a hash based on a attribute of the objects which it  
is storing.For example i am having a bean diagnosis with weightage as a  
attribute.i want to sort the hash of diagnosis based on the  
weightage.how to do this .  
can any one explain me …

thanks and regards,  
senthil kumar

---

<div class="post-metadata">

**Author:** ![senthil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c89c15/32.png) [@senthil](https://www.ruby-forum.com/u/senthil)\
**Post date:** [March 13, 2007, 11:06am UTC](https://www.ruby-forum.com/t/sorting-a-hash-based-on-a-attribute-of-teh-objects-which-it/88647/2 "2007-03-13T11:06:41Z")

</div>

senthil schrieb:

> HI all,  
> I want to sort a hash based on a attribute of the objects which it  
> is storing.For example i am having a bean diagnosis with weightage as a  
> attribute.i want to sort the hash of diagnosis based on the  
> weightage.how to do this .  
> can any one explain me …
> 
> thanks and regards,  
> senthil kumar

Here is all you need:

> **[O'Reilly Media - Technology and Business Training](https://www.oreilly.com)**
>
> Gain technology and business knowledge and hone your skills with learning resources created and curated by O'Reilly's experts: live online training, video, books, our platform has content from 200+ of the world's best publishers.

---

<div class="post-metadata">

**Author:** ![senthil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c89c15/32.png) [@senthil](https://www.ruby-forum.com/u/senthil)\
**Post date:** [March 13, 2007, 11:09am UTC](https://www.ruby-forum.com/t/sorting-a-hash-based-on-a-attribute-of-teh-objects-which-it/88647/3 "2007-03-13T11:09:42Z")

</div>

On 3/13/07, senthil [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

Hashes do not define any order by definition (in Ruby), please search  
the list for implementations of “ordered Maps/Dictionaries/Hashes”  
this is a VFAQ.

If you can take the time to convert your hash to a sorted array of  
pairs you can something like this of course.

hash.to\_a.sort{ |h1, h2|  
h1.first.whatever \<=\> h2.first.whatever # for keys  
or  
h1.last.whatelse \<=\> h2.last.whateles # for values  
}

HTH  
Robert

---

<div class="post-metadata">

**Author:** ![senthil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c89c15/32.png) [@senthil](https://www.ruby-forum.com/u/senthil)\
**Post date:** [March 13, 2007, 4:19pm UTC](https://www.ruby-forum.com/t/sorting-a-hash-based-on-a-attribute-of-teh-objects-which-it/88647/4 "2007-03-13T16:19:39Z")

</div>

On Mar 13, 2007, at 5:45 AM, senthil wrote:

> HI all,  
> I want to sort a hash based on a attribute of the objects  
> which it  
> is storing.For example i am having a bean diagnosis with weightage  
> as a  
> attribute.i want to sort the hash of diagnosis based on the  
> weightage.how to do this .  
> can any one explain me …

Hashes themselves aren’t sorted so you have to extract the  
objects and then sort that list:

class Beans \< Struct.new(:description, :weightage); end

b1 = Beans.new(‘bean 1’, 20)  
b2 = Beans.new(‘bean 2’, 30)  
b3 = Beans.new(‘bean 3’, 25)

collection = { b1.description =\> b1, b2.description =\> b2,  
b3.description =\> b3}

sorted = collection.values.sort\_by { |x| x.weightage }  
p sorted

[#\<struct Beans description=“bean 1”, weightage=20\>, #\<struct Beans  
description=“bean 3”, weightage=25\>, #\<struct Beans description=“bean  
2”, weightage=30\>]

P.S. What is a ‘bean diagnosis’ and ‘weightage’?

Gary W.

---

<div class="post-metadata">

**Author:** ![senthil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c89c15/32.png) [@senthil](https://www.ruby-forum.com/u/senthil)\
**Post date:** [March 13, 2007, 8:01pm UTC](https://www.ruby-forum.com/t/sorting-a-hash-based-on-a-attribute-of-teh-objects-which-it/88647/5 "2007-03-13T20:01:21Z")

</div>

Gary W. wrote:

> On Mar 13, 2007, at 5:45 AM, senthil wrote:
> 
> > HI all,  
> > I want to sort a hash based on a attribute of the objects which it  
> > is storing.For example i am having a bean diagnosis with weightage as a  
> > attribute.i want to sort the hash of diagnosis based on the  
> > weightage.how to do this .  
> > …  
> > P.S. What is a ‘bean diagnosis’ and ‘weightage’?

Quality control in the Jelly Belly factory?
