# How to sort my hash

**URL:** <https://www.ruby-forum.com/t/how-to-sort-my-hash/165563>\
**Category:** Ruby\
**Created:** [April 15, 2009, 10:54am UTC](https://www.ruby-forum.com/t/how-to-sort-my-hash/165563 "2009-04-15T10:54:29Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![prince\_tambar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/8dc957/32.png) [@prince\_tambar](https://www.ruby-forum.com/u/prince_tambar)\
**Post date:** [April 15, 2009, 10:54am UTC](https://www.ruby-forum.com/t/how-to-sort-my-hash/165563/1 "2009-04-15T10:54:29Z")

</div>

[#\<WorkOrder id: 8, initiator\_id: 39, is\_marked: false,  
work\_order\_type\_id: 1, descript: “\<p class=”\“MsoNormal\”"  
style=""\>\<span s…", current\_owner\_id: 39, created\_by: 39, client\_id:  
9, is\_active: nil, subject: “My new work order”, based\_on\_standard:  
false, standard\_id: 1, client\_contact\_id: 39, invoicing\_phase\_id: 1,  
impact\_due: “2009-04-16 00:00:00”, req\_compl\_date: nil,  
business\_continuity: true, track\_it: nil, is\_closed: nil, created\_at:  
“2009-04-14 17:09:45”, updated\_at: “2009-04-14 17:09:45”, summary\_date:  
nil\>, #\<WorkOrder id: 9, initiator\_id: 39, is\_marked: false,  
work\_order\_type\_id: 2, descript: "\<span style=“font-family: ‘Helvetica  
Neue’; line-he…”, current\_owner\_id: 39, created\_by: 39, client\_id: 2,  
is\_active: nil, subject: “Note book Work order”, based\_on\_standard:  
false, standard\_id: 2, client\_contact\_id: 2, invoicing\_phase\_id: 1,  
impact\_due: “2009-04-16 00:00:00”, req\_compl\_date: nil,  
business\_continuity: true, track\_it: nil, is\_closed: nil, created\_at:  
“2009-04-14 17:10:25”, updated\_at: “2009-04-14 17:10:25”, summary\_date:  
nil\>, #\<WorkOrder id: 10, initiator\_id: 39, is\_marked: false,  
work\_order\_type\_id: 2, descript: "\<span style=“font-family: ‘Helvetica  
Neue’; line-he…”, current\_owner\_id: 39, created\_by: 39, client\_id: 30,  
is\_active: nil, subject: “Rahm work order “, based\_on\_standard: false,  
standard\_id: 2, client\_contact\_id: 61, invoicing\_phase\_id: 2,  
impact\_due: “2009-04-16 00:00:00”, req\_compl\_date: nil,  
business\_continuity: false, track\_it: nil, is\_closed: nil, created\_at:  
“2009-04-14 17:11:32”, updated\_at: “2009-04-14 17:11:32”, summary\_date:  
nil\>, #\<WorkOrder id: 11, initiator\_id: 39, is\_marked: false,  
work\_order\_type\_id: 1, descript: “\<p class=\“MsoNormal\”  
style=\”\”\>\<span style=\“fon…”, current\_owner\_id: 39,  
created\_by: 39, client\_id: 25, is\_active: nil, subject: “Commerce bank  
work order”, based\_on\_standard: false, standard\_id: 1,  
client\_contact\_id: 57, invoicing\_phase\_id: 2, impact\_due: “2009-04-16  
00:00:00”, req\_compl\_date: nil, business\_continuity: false, track\_it:  
nil, is\_closed: nil, created\_at: “2009-04-14 17:12:30”, updated\_at:  
“2009-04-14 17:12:30”, summary\_date: nil\>, #\<WorkOrder id: 12,  
initiator\_id: 39, is\_marked: false, work\_order\_type\_id: 1, descript: “\<p  
class=\“MsoNormal\” style=\”\”\>\<span style=\“fon…”,  
current\_owner\_id: 39, created\_by: 39, client\_id: 9, is\_active: nil,  
subject: “Shawn work order for centeral bank”, based\_on\_standard: false,  
standard\_id: 1, client\_contact\_id: 39, invoicing\_phase\_id: 2,  
impact\_due: “2009-04-16 00:00:00”, req\_compl\_date: nil,  
business\_continuity: false, track\_it: nil, is\_closed: nil, created\_at:  
“2009-04-14 17:14:51”, updated\_at: “2009-04-14 17:14:51”, summary\_date:  
nil\>, #\<WorkOrder id: 13, initiator\_id: 39, is\_marked: false,  
work\_order\_type\_id: 2, descript: "\<span style=“font-family: ‘Helvetica  
Neue’; line-he…”, current\_owner\_id: 39, created\_by: 39, client\_id: 32,  
is\_active: nil, subject: “Work order for lorie for security sevice”,  
based\_on\_standard: false, standard\_id: 2, client\_contact\_id: 62,  
invoicing\_phase\_id: 2, impact\_due: “2009-04-16 00:00:00”,  
req\_compl\_date: nil, business\_continuity: false, track\_it: nil,  
is\_closed: nil, created\_at: “2009-04-14 17:16:30”, updated\_at:  
“2009-04-14 17:16:30”, summary\_date: nil\>, #\<WorkOrder id: 14,  
initiator\_id: 39, is\_marked: false, work\_order\_type\_id: 2, descript:  
"\<span style=“font-family: ‘Helvetica Neue’; line-he…”,  
current\_owner\_id: 39, created\_by: 39, client\_id: 23, is\_active: nil,  
subject: “Good work order for kinecta”, based\_on\_standard: false,  
standard\_id: 2, client\_contact\_id: 64, invoicing\_phase\_id: 1,  
impact\_due: “2009-04-17 00:00:00”, req\_compl\_date: nil,  
business\_continuity: true, track\_it: nil, is\_closed: nil, created\_at:  
“2009-04-15 13:05:32”, updated\_at: “2009-04-15 13:05:32”, summary\_date:  
nil\>]

this is my hash … i want to sort this by ‘business\_continuity’

please help me for this

---

<div class="post-metadata">

**Author:** ![prince\_tambar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/8dc957/32.png) [@prince\_tambar](https://www.ruby-forum.com/u/prince_tambar)\
**Post date:** [April 15, 2009, 11:23am UTC](https://www.ruby-forum.com/t/how-to-sort-my-hash/165563/2 "2009-04-15T11:23:50Z")

</div>

Sukhwinder Tambar wrote:

> this is my hash … i want to sort this by ‘business\_continuity’

Actually it’s an Array.

Both Hash and Array are Enumerable, and Enumerable has sort\_by method.

work\_orders = work\_orders.sort\_by { |e| e.business\_continuity }

However, false and true are not comparable, so you need to substitute  
some values which do. For example, if you want false to sort before  
true, then:

work\_orders = work\_orders.sort\_by { |e| e.business\_continuity ? 1 : 0  
}

If it’s really a hash of key=\>record, then

my\_hash.sort\_by { |k,v| v.business\_continuity ? 1 : 0 }

---

<div class="post-metadata">

**Author:** ![prince\_tambar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/8dc957/32.png) [@prince\_tambar](https://www.ruby-forum.com/u/prince_tambar)\
**Post date:** [April 15, 2009, 11:51am UTC](https://www.ruby-forum.com/t/how-to-sort-my-hash/165563/3 "2009-04-15T11:51:48Z")

</div>

Brian C. wrote:

> Sukhwinder Tambar wrote:
> 
> > this is my hash … i want to sort this by ‘business\_continuity’
> 
> Actually it’s an Array.
> 
> Both Hash and Array are Enumerable, and Enumerable has sort\_by method.
> 
> work\_orders = work\_orders.sort\_by { |e| e.business\_continuity }
> 
> However, false and true are not comparable, so you need to substitute  
> some values which do. For example, if you want false to sort before  
> true, then:
> 
> work\_orders = work\_orders.sort\_by { |e| e.business\_continuity ? 1 : 0  
> }
> 
> If it’s really a hash of key=\>record, then
> 
> my\_hash.sort\_by { |k,v| v.business\_continuity ? 1 : 0 }

i try my\_hash.sort\_by { |k,v| v.business\_continuity ? 1 : 0 }

this give me following error

merb : worker (port 4000) ~ undefined method `business\_continuity’ for  
nil:NilClass - (NoMethodError)

---

<div class="post-metadata">

**Author:** ![prince\_tambar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/8dc957/32.png) [@prince\_tambar](https://www.ruby-forum.com/u/prince_tambar)\
**Post date:** [April 15, 2009, 12:23pm UTC](https://www.ruby-forum.com/t/how-to-sort-my-hash/165563/4 "2009-04-15T12:23:42Z")

</div>

> 

You need to replace “my\_hash” for the name of your variable of course.

---

<div class="post-metadata">

**Author:** ![prince\_tambar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/8dc957/32.png) [@prince\_tambar](https://www.ruby-forum.com/u/prince_tambar)\
**Post date:** [April 15, 2009, 2:26pm UTC](https://www.ruby-forum.com/t/how-to-sort-my-hash/165563/5 "2009-04-15T14:26:49Z")

</div>

On Wed, Apr 15, 2009 at 12:18 PM, Sukhwinder Tambar  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:

> Ben L. wrote:
> 
> > > 
> > 
> > You need to replace “my\_hash” for the name of your variable of course.
> 
> ## yes i use mine hash name instead of my\_hash

Looks like you’re calling it on a nil reference then in that case. Check  
the  
rest of your code.

---

<div class="post-metadata">

**Author:** ![prince\_tambar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/8dc957/32.png) [@prince\_tambar](https://www.ruby-forum.com/u/prince_tambar)\
**Post date:** [April 15, 2009, 1:18pm UTC](https://www.ruby-forum.com/t/how-to-sort-my-hash/165563/6 "2009-04-15T13:18:22Z")

</div>

Ben L. wrote:

> > 
> 
> You need to replace “my\_hash” for the name of your variable of course.

yes i use mine hash name instead of my\_hash

---

<div class="post-metadata">

**Author:** ![prince\_tambar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/8dc957/32.png) [@prince\_tambar](https://www.ruby-forum.com/u/prince_tambar)\
**Post date:** [April 15, 2009, 2:52pm UTC](https://www.ruby-forum.com/t/how-to-sort-my-hash/165563/7 "2009-04-15T14:52:44Z")

</div>

Sukhwinder Tambar wrote:

> i try my\_hash.sort\_by { |k,v| v.business\_continuity ? 1 : 0 }
> 
> this give me following error
> 
> merb : worker (port 4000) ~ undefined method `business\_continuity’ for  
> nil:NilClass - (NoMethodError)

This means that v is nil. That is, your hash has a key pointing to a nil  
value.

Try:

my\_hash.each { |k,v| puts “key=#{k.inspect}, val=#{v.inspect}” }

to see what k,v pairs you are getting.

But what you showed above was definitely not a hash, it was an array.  
Hashes look like  
{key=\>value, key=\>value, …}

arrays look like  
[value, value, …]
