[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

how to sort my hash

Sukhwinder Tambar

4/15/2009 8:55:00 AM

[#<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
--
Posted via http://www.ruby-....

6 Answers

Brian Candler

4/15/2009 9:24:00 AM

0

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 }
--
Posted via http://www.ruby-....

Sukhwinder Tambar

4/15/2009 9:52:00 AM

0

Brian Candler 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)
--
Posted via http://www.ruby-....

Ben Lovell

4/15/2009 10:23:00 AM

0

[Note: parts of this message were removed to make it a legal post.]

>
>
> 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)
> --
> Posted via http://www.ruby-....
>
>
You need to replace "my_hash" for the name of your variable of course.

Sukhwinder Tambar

4/15/2009 11:18:00 AM

0

Ben Lovell 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
--
Posted via http://www.ruby-....

Ben Lovell

4/15/2009 12:26:00 PM

0

[Note: parts of this message were removed to make it a legal post.]

On Wed, Apr 15, 2009 at 12:18 PM, Sukhwinder Tambar
<princetambar@gmail.com>wrote:

> Ben Lovell 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 Candler

4/15/2009 12:53:00 PM

0

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, ...]
--
Posted via http://www.ruby-....