Selecting the highest value in attribute from hash of arrays
I have this hash:
h = {
124 => ["shoes", "59.99"],
456 => ["pants", "49.50"],
352 => ["socks", "3.99"]
}
Each value has two elements. They are a name (e.g., "shoes", "pants", "socks") and a price (e.g., "59.99", "49.50", and "3.99"). I need to select the value that has the highest price. That would be key 124 with price "59.99". How do I select the hash with the highest price?
I tried this:
h.select{ |x| x[1] }.max
#=> [456, ["pants", "49.50"]]
But this gives me the max value and returns the key 456.
arrays ruby hash
add a comment |
I have this hash:
h = {
124 => ["shoes", "59.99"],
456 => ["pants", "49.50"],
352 => ["socks", "3.99"]
}
Each value has two elements. They are a name (e.g., "shoes", "pants", "socks") and a price (e.g., "59.99", "49.50", and "3.99"). I need to select the value that has the highest price. That would be key 124 with price "59.99". How do I select the hash with the highest price?
I tried this:
h.select{ |x| x[1] }.max
#=> [456, ["pants", "49.50"]]
But this gives me the max value and returns the key 456.
arrays ruby hash
add a comment |
I have this hash:
h = {
124 => ["shoes", "59.99"],
456 => ["pants", "49.50"],
352 => ["socks", "3.99"]
}
Each value has two elements. They are a name (e.g., "shoes", "pants", "socks") and a price (e.g., "59.99", "49.50", and "3.99"). I need to select the value that has the highest price. That would be key 124 with price "59.99". How do I select the hash with the highest price?
I tried this:
h.select{ |x| x[1] }.max
#=> [456, ["pants", "49.50"]]
But this gives me the max value and returns the key 456.
arrays ruby hash
I have this hash:
h = {
124 => ["shoes", "59.99"],
456 => ["pants", "49.50"],
352 => ["socks", "3.99"]
}
Each value has two elements. They are a name (e.g., "shoes", "pants", "socks") and a price (e.g., "59.99", "49.50", and "3.99"). I need to select the value that has the highest price. That would be key 124 with price "59.99". How do I select the hash with the highest price?
I tried this:
h.select{ |x| x[1] }.max
#=> [456, ["pants", "49.50"]]
But this gives me the max value and returns the key 456.
arrays ruby hash
arrays ruby hash
edited Nov 21 '18 at 7:45
Stefan
75.4k894142
75.4k894142
asked Nov 20 '18 at 21:49
Tobyrrr00Tobyrrr00
192
192
add a comment |
add a comment |
4 Answers
4
active
oldest
votes
The most idiomatic would probably be this:
h.max_by { |_, v| v.last.to_f }
#=> [124, ["shoes", "59.99"]]
I suggest you show the return value:#=> [124, ["shoes", "59.99"]].
– Cary Swoveland
Nov 21 '18 at 2:39
add a comment |
You can dig into the structure using parentheses like this:
h = {
124 => ["shoes", "59.99"],
456 => ["pants", "49.50"],
352 => ["socks", "3.99"]
}
h.max_by{|_, (_, price)| price.to_f}
# => [124, ["shoes", "59.99"]]
add a comment |
You can first sort the hash:
sorted = hash.sort_by { |key, value| value[1].to_f }
# => [[352, ["socks", "3.99"]], [456, ["pants", "49.50"]], [124, ["shoes", "59.99"]]]
And then you can select the last pair:
sorted[-1]
# => [124, ["shoes", "59.99"]]
6
Or usingmax_by,hash.max_by { |k,v| v.last.to_f }
– Marcin Kołodziej
Nov 20 '18 at 22:04
Thank you guys. You're a lifesaver.
– Tobyrrr00
Nov 20 '18 at 22:28
1
You are sorting the values alphabetically, not by their numerical value.
– Stefan
Nov 21 '18 at 7:42
add a comment |
If you don’t mind the keys, another option could be:
h.values.map(&:reverse).max
#=> ["59.99", "shoes"]
Notice that this compares the prices as strings, not as floats, and it is not certain whether the OP wanted that.
– sawa
Nov 21 '18 at 3:12
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
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%2f53402104%2fselecting-the-highest-value-in-attribute-from-hash-of-arrays%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
The most idiomatic would probably be this:
h.max_by { |_, v| v.last.to_f }
#=> [124, ["shoes", "59.99"]]
I suggest you show the return value:#=> [124, ["shoes", "59.99"]].
– Cary Swoveland
Nov 21 '18 at 2:39
add a comment |
The most idiomatic would probably be this:
h.max_by { |_, v| v.last.to_f }
#=> [124, ["shoes", "59.99"]]
I suggest you show the return value:#=> [124, ["shoes", "59.99"]].
– Cary Swoveland
Nov 21 '18 at 2:39
add a comment |
The most idiomatic would probably be this:
h.max_by { |_, v| v.last.to_f }
#=> [124, ["shoes", "59.99"]]
The most idiomatic would probably be this:
h.max_by { |_, v| v.last.to_f }
#=> [124, ["shoes", "59.99"]]
edited Nov 21 '18 at 7:46
Stefan
75.4k894142
75.4k894142
answered Nov 21 '18 at 1:24
Michael KohlMichael Kohl
57.2k10112138
57.2k10112138
I suggest you show the return value:#=> [124, ["shoes", "59.99"]].
– Cary Swoveland
Nov 21 '18 at 2:39
add a comment |
I suggest you show the return value:#=> [124, ["shoes", "59.99"]].
– Cary Swoveland
Nov 21 '18 at 2:39
I suggest you show the return value:
#=> [124, ["shoes", "59.99"]].– Cary Swoveland
Nov 21 '18 at 2:39
I suggest you show the return value:
#=> [124, ["shoes", "59.99"]].– Cary Swoveland
Nov 21 '18 at 2:39
add a comment |
You can dig into the structure using parentheses like this:
h = {
124 => ["shoes", "59.99"],
456 => ["pants", "49.50"],
352 => ["socks", "3.99"]
}
h.max_by{|_, (_, price)| price.to_f}
# => [124, ["shoes", "59.99"]]
add a comment |
You can dig into the structure using parentheses like this:
h = {
124 => ["shoes", "59.99"],
456 => ["pants", "49.50"],
352 => ["socks", "3.99"]
}
h.max_by{|_, (_, price)| price.to_f}
# => [124, ["shoes", "59.99"]]
add a comment |
You can dig into the structure using parentheses like this:
h = {
124 => ["shoes", "59.99"],
456 => ["pants", "49.50"],
352 => ["socks", "3.99"]
}
h.max_by{|_, (_, price)| price.to_f}
# => [124, ["shoes", "59.99"]]
You can dig into the structure using parentheses like this:
h = {
124 => ["shoes", "59.99"],
456 => ["pants", "49.50"],
352 => ["socks", "3.99"]
}
h.max_by{|_, (_, price)| price.to_f}
# => [124, ["shoes", "59.99"]]
answered Nov 21 '18 at 2:47
sawasawa
130k29201301
130k29201301
add a comment |
add a comment |
You can first sort the hash:
sorted = hash.sort_by { |key, value| value[1].to_f }
# => [[352, ["socks", "3.99"]], [456, ["pants", "49.50"]], [124, ["shoes", "59.99"]]]
And then you can select the last pair:
sorted[-1]
# => [124, ["shoes", "59.99"]]
6
Or usingmax_by,hash.max_by { |k,v| v.last.to_f }
– Marcin Kołodziej
Nov 20 '18 at 22:04
Thank you guys. You're a lifesaver.
– Tobyrrr00
Nov 20 '18 at 22:28
1
You are sorting the values alphabetically, not by their numerical value.
– Stefan
Nov 21 '18 at 7:42
add a comment |
You can first sort the hash:
sorted = hash.sort_by { |key, value| value[1].to_f }
# => [[352, ["socks", "3.99"]], [456, ["pants", "49.50"]], [124, ["shoes", "59.99"]]]
And then you can select the last pair:
sorted[-1]
# => [124, ["shoes", "59.99"]]
6
Or usingmax_by,hash.max_by { |k,v| v.last.to_f }
– Marcin Kołodziej
Nov 20 '18 at 22:04
Thank you guys. You're a lifesaver.
– Tobyrrr00
Nov 20 '18 at 22:28
1
You are sorting the values alphabetically, not by their numerical value.
– Stefan
Nov 21 '18 at 7:42
add a comment |
You can first sort the hash:
sorted = hash.sort_by { |key, value| value[1].to_f }
# => [[352, ["socks", "3.99"]], [456, ["pants", "49.50"]], [124, ["shoes", "59.99"]]]
And then you can select the last pair:
sorted[-1]
# => [124, ["shoes", "59.99"]]
You can first sort the hash:
sorted = hash.sort_by { |key, value| value[1].to_f }
# => [[352, ["socks", "3.99"]], [456, ["pants", "49.50"]], [124, ["shoes", "59.99"]]]
And then you can select the last pair:
sorted[-1]
# => [124, ["shoes", "59.99"]]
edited Nov 21 '18 at 17:52
answered Nov 20 '18 at 21:55
jondavidjohnjondavidjohn
50.6k18100143
50.6k18100143
6
Or usingmax_by,hash.max_by { |k,v| v.last.to_f }
– Marcin Kołodziej
Nov 20 '18 at 22:04
Thank you guys. You're a lifesaver.
– Tobyrrr00
Nov 20 '18 at 22:28
1
You are sorting the values alphabetically, not by their numerical value.
– Stefan
Nov 21 '18 at 7:42
add a comment |
6
Or usingmax_by,hash.max_by { |k,v| v.last.to_f }
– Marcin Kołodziej
Nov 20 '18 at 22:04
Thank you guys. You're a lifesaver.
– Tobyrrr00
Nov 20 '18 at 22:28
1
You are sorting the values alphabetically, not by their numerical value.
– Stefan
Nov 21 '18 at 7:42
6
6
Or using
max_by, hash.max_by { |k,v| v.last.to_f }– Marcin Kołodziej
Nov 20 '18 at 22:04
Or using
max_by, hash.max_by { |k,v| v.last.to_f }– Marcin Kołodziej
Nov 20 '18 at 22:04
Thank you guys. You're a lifesaver.
– Tobyrrr00
Nov 20 '18 at 22:28
Thank you guys. You're a lifesaver.
– Tobyrrr00
Nov 20 '18 at 22:28
1
1
You are sorting the values alphabetically, not by their numerical value.
– Stefan
Nov 21 '18 at 7:42
You are sorting the values alphabetically, not by their numerical value.
– Stefan
Nov 21 '18 at 7:42
add a comment |
If you don’t mind the keys, another option could be:
h.values.map(&:reverse).max
#=> ["59.99", "shoes"]
Notice that this compares the prices as strings, not as floats, and it is not certain whether the OP wanted that.
– sawa
Nov 21 '18 at 3:12
add a comment |
If you don’t mind the keys, another option could be:
h.values.map(&:reverse).max
#=> ["59.99", "shoes"]
Notice that this compares the prices as strings, not as floats, and it is not certain whether the OP wanted that.
– sawa
Nov 21 '18 at 3:12
add a comment |
If you don’t mind the keys, another option could be:
h.values.map(&:reverse).max
#=> ["59.99", "shoes"]
If you don’t mind the keys, another option could be:
h.values.map(&:reverse).max
#=> ["59.99", "shoes"]
answered Nov 20 '18 at 22:34
iGianiGian
3,6452622
3,6452622
Notice that this compares the prices as strings, not as floats, and it is not certain whether the OP wanted that.
– sawa
Nov 21 '18 at 3:12
add a comment |
Notice that this compares the prices as strings, not as floats, and it is not certain whether the OP wanted that.
– sawa
Nov 21 '18 at 3:12
Notice that this compares the prices as strings, not as floats, and it is not certain whether the OP wanted that.
– sawa
Nov 21 '18 at 3:12
Notice that this compares the prices as strings, not as floats, and it is not certain whether the OP wanted that.
– sawa
Nov 21 '18 at 3:12
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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%2f53402104%2fselecting-the-highest-value-in-attribute-from-hash-of-arrays%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