# Array of hashes: finding hash with min value for key

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

``` sections < "Purchases", :owed => 840.64, :apr => 0.2474 } sections < "Transfers", :owed => 3453.45, :apr => 0.1774 } ```

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

Hi –

On Tue, 4 Sep 2007, Patrick Cotner wrote:

sections << { :type => “Transfers”, :owed => 3453.45, :apr => 0.1774 }

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

Patrick Cotner wrote:

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

``` sections < "Purchases", :owed => 840.64, :apr => 0.2474 } sections < "Transfers", :owed => 3453.45, :apr => 0.1774 } ```

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

Stefan R. wrote:

Patrick Cotner wrote:

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

``` sections < "Purchases", :owed => 840.64, :apr => 0.2474 } sections < "Transfers", :owed => 3453.45, :apr => 0.1774 } ```

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!