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










share|improve this question
























  • 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

















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










share|improve this question
























  • 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















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










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 at 0:42

























asked Nov 19 at 0:17







user10672171



















  • 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




















  • 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


















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














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





share|improve this answer























    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',
    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%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
























    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





    share|improve this answer



























      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





      share|improve this answer

























        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





        share|improve this answer














        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






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 19 at 3:08

























        answered Nov 19 at 0:33









        Sebastian Palma

        15.1k41933




        15.1k41933






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            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





















































            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

            Create new schema in PostgreSQL using DBeaver

            Deepest pit of an array with Javascript: test on Codility

            Costa Masnaga