How to sort my hash

[#<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

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 }

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)

You need to replace “my_hash” for the name of your variable of course.

On Wed, Apr 15, 2009 at 12:18 PM, Sukhwinder Tambar
[email protected]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.

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

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, …]