Probably a very simple answer to this question, but I canβt let this figure out in my life at the moment. If I have a ruby ββarray of a certain type of objects and they all have a specific field, how can I find the element of the array that has the highest value for this field?
array.max_by do |element| element.field end
Or:
array.max_by(&:field)
Does it help?
my_array.max {|a,b| a.attr <=> b.attr }
(I assume your field is named attr )
attr
You can also sort the array and then get the maximum, minimum, second largest value, etc.
array = array.sort_by {|k,v| v}.reverse puts hash[0]["key"]