Selecting the highest value in attribute from hash of arrays












2















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.










share|improve this question





























    2















    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.










    share|improve this question



























      2












      2








      2








      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.










      share|improve this question
















      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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 21 '18 at 7:45









      Stefan

      75.4k894142




      75.4k894142










      asked Nov 20 '18 at 21:49









      Tobyrrr00Tobyrrr00

      192




      192
























          4 Answers
          4






          active

          oldest

          votes


















          6














          The most idiomatic would probably be this:



          h.max_by { |_, v| v.last.to_f }
          #=> [124, ["shoes", "59.99"]]





          share|improve this answer


























          • I suggest you show the return value: #=> [124, ["shoes", "59.99"]].

            – Cary Swoveland
            Nov 21 '18 at 2:39



















          5














          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"]]





          share|improve this answer































            3














            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"]]





            share|improve this answer





















            • 6





              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






            • 1





              You are sorting the values alphabetically, not by their numerical value.

              – Stefan
              Nov 21 '18 at 7:42



















            1














            If you don’t mind the keys, another option could be:



            h.values.map(&:reverse).max



            #=> ["59.99", "shoes"]






            share|improve this answer
























            • 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











            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
            });


            }
            });














            draft saved

            draft discarded


















            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









            6














            The most idiomatic would probably be this:



            h.max_by { |_, v| v.last.to_f }
            #=> [124, ["shoes", "59.99"]]





            share|improve this answer


























            • I suggest you show the return value: #=> [124, ["shoes", "59.99"]].

              – Cary Swoveland
              Nov 21 '18 at 2:39
















            6














            The most idiomatic would probably be this:



            h.max_by { |_, v| v.last.to_f }
            #=> [124, ["shoes", "59.99"]]





            share|improve this answer


























            • I suggest you show the return value: #=> [124, ["shoes", "59.99"]].

              – Cary Swoveland
              Nov 21 '18 at 2:39














            6












            6








            6







            The most idiomatic would probably be this:



            h.max_by { |_, v| v.last.to_f }
            #=> [124, ["shoes", "59.99"]]





            share|improve this answer















            The most idiomatic would probably be this:



            h.max_by { |_, v| v.last.to_f }
            #=> [124, ["shoes", "59.99"]]






            share|improve this answer














            share|improve this answer



            share|improve this answer








            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



















            • 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













            5














            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"]]





            share|improve this answer




























              5














              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"]]





              share|improve this answer


























                5












                5








                5







                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"]]





                share|improve this answer













                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"]]






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 21 '18 at 2:47









                sawasawa

                130k29201301




                130k29201301























                    3














                    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"]]





                    share|improve this answer





















                    • 6





                      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






                    • 1





                      You are sorting the values alphabetically, not by their numerical value.

                      – Stefan
                      Nov 21 '18 at 7:42
















                    3














                    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"]]





                    share|improve this answer





















                    • 6





                      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






                    • 1





                      You are sorting the values alphabetically, not by their numerical value.

                      – Stefan
                      Nov 21 '18 at 7:42














                    3












                    3








                    3







                    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"]]





                    share|improve this answer















                    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"]]






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Nov 21 '18 at 17:52

























                    answered Nov 20 '18 at 21:55









                    jondavidjohnjondavidjohn

                    50.6k18100143




                    50.6k18100143








                    • 6





                      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






                    • 1





                      You are sorting the values alphabetically, not by their numerical value.

                      – Stefan
                      Nov 21 '18 at 7:42














                    • 6





                      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






                    • 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











                    1














                    If you don’t mind the keys, another option could be:



                    h.values.map(&:reverse).max



                    #=> ["59.99", "shoes"]






                    share|improve this answer
























                    • 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
















                    1














                    If you don’t mind the keys, another option could be:



                    h.values.map(&:reverse).max



                    #=> ["59.99", "shoes"]






                    share|improve this answer
























                    • 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














                    1












                    1








                    1







                    If you don’t mind the keys, another option could be:



                    h.values.map(&:reverse).max



                    #=> ["59.99", "shoes"]






                    share|improve this answer













                    If you don’t mind the keys, another option could be:



                    h.values.map(&:reverse).max



                    #=> ["59.99", "shoes"]







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    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



















                    • 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


















                    draft saved

                    draft discarded




















































                    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.




                    draft saved


                    draft discarded














                    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





















































                    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







                    Popular posts from this blog

                    Paul Cézanne

                    UIScrollView CustomStickyHeader Resize height generates problems when scroll is too fast

                    Angular material date-picker (MatDatepicker) auto completes the date on focus out