Forum: Ruby how to sort my hash

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Sukhwinder T. (Guest)
on 2009-04-15 12:54
[#<WorkOrder id: 8, initiator_id: 39, is_marked: false,
work_order_type_id: 1, descript: "<p class=\"\\&quot;MsoNormal\\&quot;\"
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
Brian C. (Guest)
on 2009-04-15 13:23
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 }
Sukhwinder T. (Guest)
on 2009-04-15 13:51
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)
Ben L. (Guest)
on 2009-04-15 14:23
(Received via mailing list)
>
>
You need to replace "my_hash" for the name of your variable of course.
Sukhwinder T. (Guest)
on 2009-04-15 15:18
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
Ben L. (Guest)
on 2009-04-15 16:26
(Received via mailing list)
On Wed, Apr 15, 2009 at 12:18 PM, Sukhwinder Tambar
<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.
Brian C. (Guest)
on 2009-04-15 16:52
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, ...]
This topic is locked and can not be replied to.