array of hashes: finding hash with min value for key

P

Patrick Cotner

I'm trying to write a proggy to help me figure out how long it will take
to pay off a credit card based on a payment value.
The card can have multiple sections with different owed amounts and
aprs.
Since the credit card companies pay off lower interest sections prior to
higher, I need to be able to determine which section has lowest apr. For
example

<code>
sections << { :type => "Purchases", :eek:wed => 840.64, :apr => 0.2474 }
sections << { :type => "Transfers", :eek:wed => 3453.45, :apr => 0.1774 }

</code>

I've set up a loop to iterate through each section and apply a finance
charge to the owed amounts. I have a variable with my payment amount.
After applying the finance charge, i need to see if section[:apr] is the
lowest of all sections, in order to apply the payment to that section
only.
How would this be done?

-Patrick
 
D

dblack

Hi --

I'm trying to write a proggy to help me figure out how long it will take
to pay off a credit card based on a payment value.
The card can have multiple sections with different owed amounts and
aprs.
Since the credit card companies pay off lower interest sections prior to
higher, I need to be able to determine which section has lowest apr. For
example

<code>
sections << { :type => "Purchases", :eek:wed => 840.64, :apr => 0.2474 }
sections << { :type => "Transfers", :eek:wed => 3453.45, :apr => 0.1774 }

</code>

I've set up a loop to iterate through each section and apply a finance
charge to the owed amounts. I have a variable with my payment amount.
After applying the finance charge, i need to see if section[:apr] is the
lowest of all sections, in order to apply the payment to that section
only.
How would this be done?

You could say:

lower = sections.find {|s| s[:apr] < section[:apr] }

and if that's non-nil, you've found a lower one.


David

--
* Books:
RAILS ROUTING (new! http://www.awprofessional.com/title/0321509242)
RUBY FOR RAILS (http://www.manning.com/black)
* Ruby/Rails training
& consulting: Ruby Power and Light, LLC (http://www.rubypal.com)
 
S

Stefan Rusterholz

Patrick said:
I'm trying to write a proggy to help me figure out how long it will take
to pay off a credit card based on a payment value.
The card can have multiple sections with different owed amounts and
aprs.
Since the credit card companies pay off lower interest sections prior to
higher, I need to be able to determine which section has lowest apr. For
example

<code>
sections << { :type => "Purchases", :eek:wed => 840.64, :apr => 0.2474 }
sections << { :type => "Transfers", :eek:wed => 3453.45, :apr => 0.1774 }

</code>

I've set up a loop to iterate through each section and apply a finance
charge to the owed amounts. I have a variable with my payment amount.
After applying the finance charge, i need to see if section[:apr] is the
lowest of all sections, in order to apply the payment to that section
only.
How would this be done?

-Patrick

ri Enumerable#min
sections.min { |a,b| a[:apr] <=> b[:apr] }

Regards
Stefan
 
P

Patrick Cotner

Stefan said:
Patrick said:
I'm trying to write a proggy to help me figure out how long it will take
to pay off a credit card based on a payment value.
The card can have multiple sections with different owed amounts and
aprs.
Since the credit card companies pay off lower interest sections prior to
higher, I need to be able to determine which section has lowest apr. For
example

<code>
sections << { :type => "Purchases", :eek:wed => 840.64, :apr => 0.2474 }
sections << { :type => "Transfers", :eek:wed => 3453.45, :apr => 0.1774 }

</code>

I've set up a loop to iterate through each section and apply a finance
charge to the owed amounts. I have a variable with my payment amount.
After applying the finance charge, i need to see if section[:apr] is the
lowest of all sections, in order to apply the payment to that section
only.
How would this be done?

-Patrick

ri Enumerable#min
sections.min { |a,b| a[:apr] <=> b[:apr] }

Regards
Stefan

Thanks, David, but I went with Stephan's example.
I posted the (ugly) finished code over here:
http://www.ruby-forum.com/topic/123523
Thanks, Stephan!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top