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 6780169
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:28:26+00:00 2026-05-26T16:28:26+00:00

I am new to ruby and looking for an elegant way to sort the

  • 0

I am new to ruby and looking for an elegant way to sort the following hash.

Hash A:

a = { :metric1 => {
          "Bob" =>10,
          "Jane" =>15,
          "Sally" =>20
      },                
      :metric2 => {  
          "Jane" =>15,
          "Bob" =>10,
          "Sally" =>10 
      },         
      :metric3 => {  
          "Bob" =>10,
          "Sally" =>10,
          "Jane" =>5
      }
    }

Hash B:

b = { "Bob" => {
        :metric1 =>10,
        :metric2 =>10,
        :metric3 =>10
      },
      "Jane" => {
        :metric1 =>15,
        :metric2 =>15,
        :metric3 =>5
      },
      "Sally" => {
        :metric1 =>20,
        :metric2 =>10,
        :metric3 =>10
      }
    }

Essentially I want an easy way to iterate this object to output a table like:

        Metric 1  Metric 2  Metric 3
Bob     10        20        20
Jane    15        15        5
Sally   20        10        10
  • 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-26T16:28:26+00:00Added an answer on May 26, 2026 at 4:28 pm

    Functional approach (assumes all people have the same metric keys):

    names = a.values.first.keys
    b = Hash[names.map do |name| 
      [name, Hash[a.map { |metric, values| [metric, values[person]] }]]
    end]
    

    [Edit] A second approach, proably more orthodox than my first attempt. It uses Facets (so it may look a bit cryptic for those unfamiliar the new abstractions it procides). However, it’s easy if you check the output of each step:

    triplets = a.flat_map { |metric, h| h.map { |name, value| [name, metric, value] } }
    pairs_by_name = triplets.map_by { |name, metric, value| [name, [metric, value]] }
    b = pairs_by_name.mash { |name, pairs| [name, pairs.to_h] }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Arising out of this question, I'm looking for an elegant (ruby) way to compute
I'm new to Ruby on Rails (I know Ruby just decently though) and looking
I'm new to Ruby, and I'm trying the following: mySet = numOfCuts.times.map{ rand(seqLength) }
I'm pretty new to ruby environments and I was looking for a nice logging
Am brand new to Ruby looking to get Redmine to run on WinServer08 sp1
I'm new to Ruby on Rails, looking at using it for an app that
I am looking for NEW Ruby on Rails resource websites (Screencasts, Tutorials, etc) some
I am new to Ruby. I'm looking to import functions from a module that
I'm new to Ruby and I was looking around for libraries to parse html
very new to Ruby, I've got the following situation. I have a file with

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.