Finding the Element of a Ruby Array with the Maximum Value for a Particular Attribute

Finding the element of a Ruby array with the maximum value for a particular attribute

array.max_by do |element|
element.field
end

Or:

array.max_by(&:field)

How to get the unique elements of an array with a maximum value of attribute

Something like this, maybe?

a = [
{id:1, price:10},
{id:2, price:9},
{id:3, price:8},
{id:1, price:7}
]

b = a.group_by{|h| h[:id]}.
map{|_, v| v.max_by {|el| el[:price]}}

b # => [{:id=>1, :price=>10}, {:id=>2, :price=>9}, {:id=>3, :price=>8}]

Get the index of an element in an array of hashes with the maximum value for a particular attribute

You want then

<% selected = my_array.each_index.max_by { |i| my_array[i].updated_at } %>

Max value within array of objects

Let's say you have set up the following model:

class SomeObject
attr_accessor :prop1, :prop2, :val1

def initialize(prop1, prop2, val1)
@prop1 = prop1
@prop2 = prop2
@val1 = val1
end
end

#define your objects from the class above
david = SomeObject.new('David', 'Peters', 23)
steven = SomeObject.new('Steven', 'Peters', 26)
john = SomeObject.new('John', 'Peters', 33)

#define an array of the above objects
array = [david, steven, john]

Then use max_by by passing the condition into its block as follows to determine the object with the max val1 value. Finally call val1 to get the value of the max object.

array.max_by {|e| e.val1 }.val1 #=> 33

You may also consider using hashes (negating the need to define a new class), like so:

david  = {f_name: 'David',  s_name: 'Peters', age: 23}
steven = {f_name: 'Steven', s_name: 'Peters', age: 26}
john = {f_name: 'John', s_name: 'Peters', age: 33}

array = [david, steven, john]
array.max_by { |hash| hash[:age] }[:age] #=> 33

Find max value in a class in ruby

If you have a class like this:

class Person
attr_accessor :age
def initialize(age)
@age = age
end
end

And an array like this:

people = [Person.new(10), Person.new(20), Person.new(30)]

Finding the maximum age

You can get the ages with Array#map:

people.map { |person| person.age }
#=> [10, 20, 30]

# or shorter

people.map(&:age)
#=> [10, 20, 30]

And the maximum value with Enumerable#max:

people.map(&:age).max
#=> 30

Finding the oldest person

Or you could find the oldest person with Enumerable#max_by:

oldest = people.max_by { |person| person.age }
#=> #<Person:0x007fef4991d0a8 @age=30>

# or shorter

oldest = people.max_by(&:age)
#=> #<Person:0x007fef4991d0a8 @age=30>

And his or her age with:

oldest.age
#=> 30

How to find the max value in an array of hashes?

You can do this with group_by and max:

arr.group_by { |x| x[:total] }.max.last

Find N objects in Array which have the max value in a property using Ruby

Here is one way :

max = arr.max_by{|e| e.score}
max_all = arr.find_all{|e| e.score==max}.first(3)

update

ar = [{score:10}, {score:5}, {score:7}, {score:8}]
p ar.sort_by{|e| -e[:score]}.first(3)
# => [{:score=>10}, {:score=>8}, {:score=>7}]

How to get min/max value indices in a Ruby array

arr.each_index.min_by(x) { |i| arr[i] }

or

arr.each_with_index.min(x).map(&:last)

Demo:

> arr, x = [4, 9, 0, -3, 16, 7], 4
=> [[4, 9, 0, -3, 16, 7], 4]
> arr.each_index.min_by(x) { |i| arr[i] }
=> [3, 2, 0, 5]
> arr.each_with_index.min(x).map(&:last)
=> [3, 2, 0, 5]

How do I find the index of the maximum value of an array?

If you want to omit nils from the result, then you can use:

array.index(array.compact.max)

Or if you wish to treat nils like zeros, then first convert them to Floats:

array.index(array.map(&:to_f).max)

In the event of a tie, this will return the index of the first max value. You could also get the last index with Array#rindex.

Get variable whose value is the maximum in an array

When you build an array by writing array = [a, b, c], the spots in the array do not retain any kind of association with the variables named a, b, and c, so you there is no way to do exactly what you are talking about. Those variables can change without affecting the array.

You could change the way you are storing your data in order to make this possible, but without knowing what data you are storing it is hard to recommend a good way to do it. You could consider storing your data inside a hash instead of in loose variables and an array:

hash = { a: 4, b: 7, c: 1}


Related Topics



Leave a reply



Submit