Outputting the keys of the max value of a hash within Arrays in Ruby using methods
up vote
1
down vote
favorite
data = [
"Company one" => {
"number_1" => 46,
"number_2" => 3055,
"country" => "USA"
},
"Company two" => {
"number_1" => 32,
"number_2" => 6610,
"country" => "USA"
},
"Company three" => {
"number_1" => 40,
"number_2" => 9128,
"country" => "USA"
}
]
So I have this array in which I'm trying to get which of the company has the biggest number in 'number_2'. The largest would be Company three with 9128.
So I have this code that puts the largest number which would be 9128
def number(data)
collected_array=
data.each do |company_hash|
collected_array = company_hash.map do |k,v|
v["number_2"]
end
end
puts collected_array.max
end
number(data)
But I'm trying to puts the company name with the largest number which would be "Company three". I've tried .keys
and other ways but it gives me error.
I've tried this way:
def number(data)
collected_array=
data.each do |company_hash|
collected_array = company_hash.map do |k,v|
v["number_2"]
k
end
end
puts collected_array.max
end
number(data)
but it gives me "Company two" rather than "Company three" which would be the company with the highest number
ruby methods hash max
add a comment |
up vote
1
down vote
favorite
data = [
"Company one" => {
"number_1" => 46,
"number_2" => 3055,
"country" => "USA"
},
"Company two" => {
"number_1" => 32,
"number_2" => 6610,
"country" => "USA"
},
"Company three" => {
"number_1" => 40,
"number_2" => 9128,
"country" => "USA"
}
]
So I have this array in which I'm trying to get which of the company has the biggest number in 'number_2'. The largest would be Company three with 9128.
So I have this code that puts the largest number which would be 9128
def number(data)
collected_array=
data.each do |company_hash|
collected_array = company_hash.map do |k,v|
v["number_2"]
end
end
puts collected_array.max
end
number(data)
But I'm trying to puts the company name with the largest number which would be "Company three". I've tried .keys
and other ways but it gives me error.
I've tried this way:
def number(data)
collected_array=
data.each do |company_hash|
collected_array = company_hash.map do |k,v|
v["number_2"]
k
end
end
puts collected_array.max
end
number(data)
but it gives me "Company two" rather than "Company three" which would be the company with the highest number
ruby methods hash max
Can you corroborate the format of the content insidedata
?
– Sebastian Palma
Nov 19 at 0:26
I'm sorry, I don't understand. corroborate as in?
– user10672171
Nov 19 at 0:29
It looks like it would be better as a Hash containingCompany one
,Company two
, etc. Now is a hash, and inspecting the elements, it's making more difficult to work with it.
– Sebastian Palma
Nov 19 at 0:30
while that would be better. I can't modify anything inside thedata
– user10672171
Nov 19 at 0:38
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
data = [
"Company one" => {
"number_1" => 46,
"number_2" => 3055,
"country" => "USA"
},
"Company two" => {
"number_1" => 32,
"number_2" => 6610,
"country" => "USA"
},
"Company three" => {
"number_1" => 40,
"number_2" => 9128,
"country" => "USA"
}
]
So I have this array in which I'm trying to get which of the company has the biggest number in 'number_2'. The largest would be Company three with 9128.
So I have this code that puts the largest number which would be 9128
def number(data)
collected_array=
data.each do |company_hash|
collected_array = company_hash.map do |k,v|
v["number_2"]
end
end
puts collected_array.max
end
number(data)
But I'm trying to puts the company name with the largest number which would be "Company three". I've tried .keys
and other ways but it gives me error.
I've tried this way:
def number(data)
collected_array=
data.each do |company_hash|
collected_array = company_hash.map do |k,v|
v["number_2"]
k
end
end
puts collected_array.max
end
number(data)
but it gives me "Company two" rather than "Company three" which would be the company with the highest number
ruby methods hash max
data = [
"Company one" => {
"number_1" => 46,
"number_2" => 3055,
"country" => "USA"
},
"Company two" => {
"number_1" => 32,
"number_2" => 6610,
"country" => "USA"
},
"Company three" => {
"number_1" => 40,
"number_2" => 9128,
"country" => "USA"
}
]
So I have this array in which I'm trying to get which of the company has the biggest number in 'number_2'. The largest would be Company three with 9128.
So I have this code that puts the largest number which would be 9128
def number(data)
collected_array=
data.each do |company_hash|
collected_array = company_hash.map do |k,v|
v["number_2"]
end
end
puts collected_array.max
end
number(data)
But I'm trying to puts the company name with the largest number which would be "Company three". I've tried .keys
and other ways but it gives me error.
I've tried this way:
def number(data)
collected_array=
data.each do |company_hash|
collected_array = company_hash.map do |k,v|
v["number_2"]
k
end
end
puts collected_array.max
end
number(data)
but it gives me "Company two" rather than "Company three" which would be the company with the highest number
ruby methods hash max
ruby methods hash max
edited Nov 19 at 0:42
asked Nov 19 at 0:17
user10672171
Can you corroborate the format of the content insidedata
?
– Sebastian Palma
Nov 19 at 0:26
I'm sorry, I don't understand. corroborate as in?
– user10672171
Nov 19 at 0:29
It looks like it would be better as a Hash containingCompany one
,Company two
, etc. Now is a hash, and inspecting the elements, it's making more difficult to work with it.
– Sebastian Palma
Nov 19 at 0:30
while that would be better. I can't modify anything inside thedata
– user10672171
Nov 19 at 0:38
add a comment |
Can you corroborate the format of the content insidedata
?
– Sebastian Palma
Nov 19 at 0:26
I'm sorry, I don't understand. corroborate as in?
– user10672171
Nov 19 at 0:29
It looks like it would be better as a Hash containingCompany one
,Company two
, etc. Now is a hash, and inspecting the elements, it's making more difficult to work with it.
– Sebastian Palma
Nov 19 at 0:30
while that would be better. I can't modify anything inside thedata
– user10672171
Nov 19 at 0:38
Can you corroborate the format of the content inside
data
?– Sebastian Palma
Nov 19 at 0:26
Can you corroborate the format of the content inside
data
?– Sebastian Palma
Nov 19 at 0:26
I'm sorry, I don't understand. corroborate as in?
– user10672171
Nov 19 at 0:29
I'm sorry, I don't understand. corroborate as in?
– user10672171
Nov 19 at 0:29
It looks like it would be better as a Hash containing
Company one
, Company two
, etc. Now is a hash, and inspecting the elements, it's making more difficult to work with it.– Sebastian Palma
Nov 19 at 0:30
It looks like it would be better as a Hash containing
Company one
, Company two
, etc. Now is a hash, and inspecting the elements, it's making more difficult to work with it.– Sebastian Palma
Nov 19 at 0:30
while that would be better. I can't modify anything inside the
data
– user10672171
Nov 19 at 0:38
while that would be better. I can't modify anything inside the
data
– user10672171
Nov 19 at 0:38
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
As stated by @Cary, it can be simplified accessing the first element on data, and there using max_by, on the hash local variable available within the block checking the number_2
key value.
As the result is an Array containing two elements, the first one is the company name, the second and last one, the hash containing its data:
data = [
"Company one" => {
"number_1" => 46,
"number_2" => 3055,
"country" => "USA"
},
"Company two" => {
"number_1" => 32,
"number_2" => 6610,
"country" => "USA"
},
"Company three" => {
"number_1" => 40,
"number_2" => 9128,
"country" => "USA"
}
]
max_company = data.first.max_by { |_, h| h['number_2'] }
p max_company.first # "Company three"
p max_company.last['number_2'] # 9128
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
As stated by @Cary, it can be simplified accessing the first element on data, and there using max_by, on the hash local variable available within the block checking the number_2
key value.
As the result is an Array containing two elements, the first one is the company name, the second and last one, the hash containing its data:
data = [
"Company one" => {
"number_1" => 46,
"number_2" => 3055,
"country" => "USA"
},
"Company two" => {
"number_1" => 32,
"number_2" => 6610,
"country" => "USA"
},
"Company three" => {
"number_1" => 40,
"number_2" => 9128,
"country" => "USA"
}
]
max_company = data.first.max_by { |_, h| h['number_2'] }
p max_company.first # "Company three"
p max_company.last['number_2'] # 9128
add a comment |
up vote
1
down vote
accepted
As stated by @Cary, it can be simplified accessing the first element on data, and there using max_by, on the hash local variable available within the block checking the number_2
key value.
As the result is an Array containing two elements, the first one is the company name, the second and last one, the hash containing its data:
data = [
"Company one" => {
"number_1" => 46,
"number_2" => 3055,
"country" => "USA"
},
"Company two" => {
"number_1" => 32,
"number_2" => 6610,
"country" => "USA"
},
"Company three" => {
"number_1" => 40,
"number_2" => 9128,
"country" => "USA"
}
]
max_company = data.first.max_by { |_, h| h['number_2'] }
p max_company.first # "Company three"
p max_company.last['number_2'] # 9128
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
As stated by @Cary, it can be simplified accessing the first element on data, and there using max_by, on the hash local variable available within the block checking the number_2
key value.
As the result is an Array containing two elements, the first one is the company name, the second and last one, the hash containing its data:
data = [
"Company one" => {
"number_1" => 46,
"number_2" => 3055,
"country" => "USA"
},
"Company two" => {
"number_1" => 32,
"number_2" => 6610,
"country" => "USA"
},
"Company three" => {
"number_1" => 40,
"number_2" => 9128,
"country" => "USA"
}
]
max_company = data.first.max_by { |_, h| h['number_2'] }
p max_company.first # "Company three"
p max_company.last['number_2'] # 9128
As stated by @Cary, it can be simplified accessing the first element on data, and there using max_by, on the hash local variable available within the block checking the number_2
key value.
As the result is an Array containing two elements, the first one is the company name, the second and last one, the hash containing its data:
data = [
"Company one" => {
"number_1" => 46,
"number_2" => 3055,
"country" => "USA"
},
"Company two" => {
"number_1" => 32,
"number_2" => 6610,
"country" => "USA"
},
"Company three" => {
"number_1" => 40,
"number_2" => 9128,
"country" => "USA"
}
]
max_company = data.first.max_by { |_, h| h['number_2'] }
p max_company.first # "Company three"
p max_company.last['number_2'] # 9128
edited Nov 19 at 3:08
answered Nov 19 at 0:33
Sebastian Palma
15.1k41933
15.1k41933
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53366803%2foutputting-the-keys-of-the-max-value-of-a-hash-within-arrays-in-ruby-using-metho%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Can you corroborate the format of the content inside
data
?– Sebastian Palma
Nov 19 at 0:26
I'm sorry, I don't understand. corroborate as in?
– user10672171
Nov 19 at 0:29
It looks like it would be better as a Hash containing
Company one
,Company two
, etc. Now is a hash, and inspecting the elements, it's making more difficult to work with it.– Sebastian Palma
Nov 19 at 0:30
while that would be better. I can't modify anything inside the
data
– user10672171
Nov 19 at 0:38