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

  • Home
  • SEARCH
  • 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 6023269
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T03:58:46+00:00 2026-05-23T03:58:46+00:00

I have a set of 2 or more objects that I’d like to order.

  • 0

I have a set of 2 or more objects that I’d like to order. I had been doing it like this:

card.max_by{|strength| strength.score

Where score was an integer score I had computed given some arbitrary rules. I knew this would be something I would refactor, so now I am doing so. And the “clean” way to give a score to a hand is to give it an array of values like

foo.score = [9,3,nil,4]

And compare it to another hand which might have an array like

bar.score = [5,10,12,12]

And foo <=> bar would tell me that foo is the greater array and so it should be returned by max_by. The problem is that max_by apparently won’t make comparisons on arrays. Is there another way I can do this to sort by the array value?

  • 1 1 Answer
  • 1 View
  • 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-23T03:58:47+00:00Added an answer on May 23, 2026 at 3:58 am

    If it’s just array-based ordering that you want (you really do want the spaceship operator) and you want to find the ‘biggest’ by sorting, then:

    Hand = Struct.new(:score)
    
    hands = [
     Hand.new([9,3,0,4]),
     Hand.new([8,8,8,8]),
     Hand.new([5,10,12,12]),
     Hand.new([1,99,99,99])
    ]
    
    biggest = hands.sort_by(&:score).last
    p biggest
    #=> #<struct Hand score=[9, 3, 0, 4]>
    

    If you really only need to find the largest hand, however, the following will be more efficient than ordering the entire array:

    biggest = hands.inject do |max,hand|
      if (max.score <=> hand.score) == -1
        hand
      else
        max
      end
    end
    p biggest
    #=> #<struct Hand score=[9, 3, 0, 4]>
    

    Edit: Reading your comment, if you really need multiple values that match, I would do this:

    Hand = Struct.new(:name,:score) do 
      MAX_SCORE_PART = 13 # 13 ranks in a suit
      def numeric_score
        value = 0
        score.each_with_index do |part,i|
          value += part.to_i * MAX_SCORE_PART**(score.length-i-1)
        end
        value
      end
    end
    
    hands = [
      Hand.new('Bob', [9,3,nil,4] ),
      Hand.new('Jim', [8,8,8,8]   ),
      Hand.new('Foo', [5,10,12,12]),
      Hand.new('Sam', [1,13,13,13]),
      Hand.new('Zak', [9,3,0,4]   ),
    ]
    
    require 'pp'
    by_score = hands.group_by(&:numeric_score)
    pp by_score
    #=> {20284=>
    #=>   [#<struct Hand name="Bob", score=[9, 3, nil, 4]>,
    #=>    #<struct Hand name="Zak", score=[9, 3, 0, 4]>],
    #=>  19040=>[#<struct Hand name="Jim", score=[8, 8, 8, 8]>],
    #=>  12843=>[#<struct Hand name="Foo", score=[5, 10, 12, 12]>],
    #=>  4576=>[#<struct Hand name="Sam", score=[1, 13, 13, 13]>]}
    
    pp by_score[by_score.keys.max]
    #=> [#<struct Hand name="Bob", score=[9, 3, nil, 4]>,
    #=>  #<struct Hand name="Zak", score=[9, 3, 0, 4]>]
    

    For an inject-based implementation:

    def numeric_score
      score.enum_for(:inject,0).with_index do |(val,part),i|
        val += part.to_i * MAX_SCORE_PART**(score.length-i-1)
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a set of columns that applies to more than 1 model. For
I have a collection of objects that I would like to iterate through to
I have a Python set that contains objects with __hash__ and __eq__ methods in
I have various derived objects that I would like the user to be able
Okay I have a JSON file and it has a set of objects that
I have a set of subclassed domain objects that I fetch with Linq and
Like many 3d graphical programs, I have a bunch of objects that have their
I have a set of five boolean values. If more than one of these
We have set a custom 404 error page in order to display a 'nice'
I have created objects that are interfaces to a web service. One typical object

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.