Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7433593
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T09:41:46+00:00 2026-05-29T09:41:46+00:00

Ruby has Enumerable#max and Enumerable#max_by and I am using max_by in the following way.

  • 0

Ruby has Enumerable#max and Enumerable#max_by and I am using max_by in the following way.

array = [
           {:a => 1, :b => 2, :c => 3}, 
           {:a => 3, :b => 2, :c => 4}, 
           {:a => 3, :b => 1, :c => 4}
        ]
max_item = array.max_by { |item| item[:b] }
=> {:b=>2, :c=>3, :a=>1}

So, it will return a single maximum (I think first), even though there are 2 items with same maximum value. I want to implement a function which gives both the items in such cases. Is there some other variant of max_by or do I need to write from scratch for this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T09:41:47+00:00Added an answer on May 29, 2026 at 9:41 am
    array = [
               {:a => 1, :b => 2, :c => 3}, 
               {:a => 3, :b => 2, :c => 4}, 
               {:a => 3, :b => 1, :c => 4}
            ]
    
    array.group_by{|el| el[:b]}.max.last 
    #=>[{:a=>1, :b=>2, :c=>3}, {:a=>3, :b=>2, :c=>4}]
    

    group_by works a little too hard, but it does deliver.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Ruby's Enumerable has a select which can select certain items from an array: parent=@sections.select
Does JavaScript has similar functionality as Ruby has? array.select {|x| x > 3} Something
I see that Ruby has the following variables: - global variables (represented by $variable_name)
Ruby has two different exceptions mechanisms: Throw/Catch and Raise/Rescue. Why do we have two?
Ruby has a wealth of conditional constructs, including if / unless , while /
Ruby has been around for a while now so I was wondering if there
I recently installed jruby on a machine that also has ruby installed on it.
Ruby on Rails has magic timestamping fields that are automatically updated when a record
Ruby on Rails has a lot of ways to generate JavaScript. Particularly when it
Ruby on Rails has become a new competitive face in the server programming industry,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.