# Hash Sorting

**URL:** <https://www.ruby-forum.com/t/hash-sorting/156380>\
**Category:** Rails\
**Created:** [December 26, 2008, 8:32am UTC](https://www.ruby-forum.com/t/hash-sorting/156380 "2008-12-26T08:32:15Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![sunfish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/96bed5/32.png) [@sunfish](https://www.ruby-forum.com/u/sunfish)\
**Post date:** [December 26, 2008, 8:32am UTC](https://www.ruby-forum.com/t/hash-sorting/156380/1 "2008-12-26T08:32:15Z")

</div>

Hi,

My hash contain key as a combination of album date created\_at and  
album\_id and values are some photo object.and i need to sort this hash  
based on key; but i need to sort it also considering date created\_at.  
but if i consider a key as combination of both (date creataed and  
album\_id) then it is not pure date. how i solve it? any better solution

---

<div class="post-metadata">

**Author:** ![sunfish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/96bed5/32.png) [@sunfish](https://www.ruby-forum.com/u/sunfish)\
**Post date:** [December 26, 2008, 8:49am UTC](https://www.ruby-forum.com/t/hash-sorting/156380/2 "2008-12-26T08:49:17Z")

</div>

Would you mind posting an example of the hash and an example of how you  
would like it sorted?

Thanks,  
Josh  
[http://iammrjoshua.com](http://iammrjoshua.com)

Sunny B. wrote:

> Hi,
> 
> My hash contain key as a combination of album date created\_at and  
> album\_id and values are some photo object.and i need to sort this hash  
> based on key; but i need to sort it also considering date created\_at.  
> but if i consider a key as combination of both (date creataed and  
> album\_id) then it is not pure date. how i solve it? any better solution

---

<div class="post-metadata">

**Author:** ![sunfish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/96bed5/32.png) [@sunfish](https://www.ruby-forum.com/u/sunfish)\
**Post date:** [December 26, 2008, 9:00am UTC](https://www.ruby-forum.com/t/hash-sorting/156380/3 "2008-12-26T09:00:53Z")

</div>

Joshua A. wrote:

> Would you mind posting an example of the hash and an example of how you  
> would like it sorted?
> 
> Thanks,  
> Josh  
> [http://iammrjoshua.com](http://iammrjoshua.com)

say i have album id 233 and created date 12/09/2008 so my key should be  
12092008\_233 in this way?

---

<div class="post-metadata">

**Author:** ![sunfish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/96bed5/32.png) [@sunfish](https://www.ruby-forum.com/u/sunfish)\
**Post date:** [December 27, 2008, 5:18pm UTC](https://www.ruby-forum.com/t/hash-sorting/156380/4 "2008-12-27T17:18:00Z")

</div>

Sunny B. wrote:

> Joshua A. wrote:
> 
> > Would you mind posting an example of the hash and an example of how you  
> > would like it sorted?
> > 
> > Thanks,  
> > Josh  
> > [http://iammrjoshua.com](http://iammrjoshua.com)
> 
> say i have album id 233 and created date 12/09/2008 so my key should be  
> 12092008\_233 in this way?

Instead of concatenating a string, just make your key an array of the  
values.

hash = {}  
key = [date, album\_id]  
hash[key] = value

However, hashes are not sorted in Ruby 1.8. Rails 2.x has an OrderedHash  
that maintains the keys’ nsertion order, but it’s not efficient for  
lookup by key. If you only plan to use the hash for iteration, that’s  
probably OK (but then why use a hash?).

---

<div class="post-metadata">

**Author:** ![sunfish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/96bed5/32.png) [@sunfish](https://www.ruby-forum.com/u/sunfish)\
**Post date:** [December 27, 2008, 5:22pm UTC](https://www.ruby-forum.com/t/hash-sorting/156380/5 "2008-12-27T17:22:03Z")

</div>

On Dec 27, 4:18 pm, Jeremy Weiskotten \<rails-mailing-l…@andreas-  
[s.net](http://s.net)\> wrote:

> hash = {}  
> key = [date, album\_id]  
> hash[key] = value
> 
> However, hashes are not sorted in Ruby 1.8. Rails 2.x has an OrderedHash  
> that maintains the keys’ nsertion order, but it’s not efficient for  
> lookup by key. If you only plan to use the hash for iteration, that’s

OrderedHash is a lot speedier in 2.3

Fred

---

<div class="post-metadata">

**Author:** ![sunfish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/96bed5/32.png) [@sunfish](https://www.ruby-forum.com/u/sunfish)\
**Post date:** [December 27, 2008, 5:28pm UTC](https://www.ruby-forum.com/t/hash-sorting/156380/6 "2008-12-27T17:28:55Z")

</div>

Frederick C. wrote:

> On Dec 27, 4:18ï¿½pm, Jeremy Weiskotten \<rails-mailing-l…@andreas-  
> [s.net](http://s.net)\> wrote:
> 
> > hash = {}  
> > key = [date, album\_id]  
> > hash[key] = value
> > 
> > However, hashes are not sorted in Ruby 1.8. Rails 2.x has an OrderedHash  
> > that maintains the keys’ nsertion order, but it’s not efficient for  
> > lookup by key. If you only plan to use the hash for iteration, that’s
> 
> OrderedHash is a lot speedier in 2.3
> 
> Fred

Good to know, although I’m not even on 2.2 yet due to plugin  
incompatibilities. Thanks!

---

<div class="post-metadata">

**Author:** ![sunfish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/96bed5/32.png) [@sunfish](https://www.ruby-forum.com/u/sunfish)\
**Post date:** [December 28, 2008, 5:34pm UTC](https://www.ruby-forum.com/t/hash-sorting/156380/7 "2008-12-28T17:34:30Z")

</div>

On Sat, Dec 27, 2008 at 8:28 AM, Jeremy Weiskotten  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > lookup by key. If you only plan to use the hash for iteration, that’s
> > 
> > OrderedHash is a lot speedier in 2.3
> > 
> > Fred
> 
> Good to know, although I’m not even on 2.2 yet due to plugin  
> incompatibilities. Thanks!

Jeremy,

I apologize if this was already considered, but you _can_ sort a hash  
by a key/value, but it’s not inherently sorted for you.

- 

> **[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.

Good luck!

Cheers,  
Robby

–  
Robby R.  
Chief Evangelist, Partner

PLANET ARGON, LLC  
design // development // hosting

> **[Planet Argon](https://www.planetargon.com/)**
>
> Planet Argon is a Ruby on Rails development agency specializing in maintenance and support for existing Rails apps. Based in Portland, OR, Planet Argon has been a member of the Rails community since 2004.

[http://www.robbyonrails.com/](http://www.robbyonrails.com/)  
aim: planetargon

+1 503 445 2457  
+1 877 55 ARGON [toll free]  
+1 815 642 4068 [fax]
