a = {
1 => ["walmart", "walmart.com", 300.0],
2 => ["amazon", "amazon.com", 350.0],
...
}
How do I find the element with lowest value of the float value in its array?
min_by
is available as a method from the Enumerable
module.
It gets the array of all values in the Hash, and then picks the minimum value based on the last element of each array.
a.values.min_by(&:last)
Another useful method is sort_by from the Enumerable module as well. It will arrange your hash from ascending order. Then chain the method with first to grab the lowest value.
a.sort_by { |key, value| value }.first
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With