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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T14:41:10+00:00 2026-05-28T14:41:10+00:00

I have a hash that contains a number of keys/values that need sorting by

  • 0

I have a hash that contains a number of keys/values that need sorting by their deepest value.

Here is an example of what I am dealing with:

hash = {"en"=> 
    { "key1"=> 
        {"foo1"=>1, 
         "foo2"=>2, 
         "foo3"=>1}, 
      "key2"=>
        {"foo4"=>1,
         "foo5"=>3, 
         "foo6"=>2, 
         "foo7"=>1}
    }
 "fr"=>
    ....
}

I want to key each main key (i.e. “en”, “fr”, es”) and sort by the values for the keys within. So having “en” and for “key1” to have “value2” at the top with the highest number (frequency). Then doing the same for the rest of the hash. I’m not bothered about the order of the keys “en”, “fr” etc.

So I want the output to be something like this:

hash = {"en"=> 
    { "key1"=> 
        {"foo2"=>2, 
         "foo1"=>1, 
         "foo3"=>1}, 
      "key2"=>
        {"foo5"=>3,
         "foo6"=>2, 
         "foo4"=>1, 
         "foo7"=>1}
    }
 "fr"=>
    ....
}
  • 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-28T14:41:11+00:00Added an answer on May 28, 2026 at 2:41 pm

    Fragile, but should work:

    def sort_deep(h)
      if h.values.all?{ |o| o.is_a? Numeric }
        Hash[ h.sort_by{|k,v|[-v,k]} ]
      else
        Hash[ h.map{ |k,v| [ k, sort_deep(v) ] } ]
      end
    end
    

    Proof:

    hash = {"en"=> 
        { "key1"=> 
            {"foo1"=>1, 
             "foo2"=>2, 
             "foo3"=>1}, 
          "key2"=>
            {"foo4"=>1,
             "foo5"=>3, 
             "foo6"=>2, 
             "foo7"=>1}
        },
     "fr"=>
        { "key1"=> 
            {"foo1"=>91, 
             "foo2"=>22, 
             "foo3"=>12}, 
          "key2"=>
            {"foo4"=>21,
             "foo5"=>31, 
             "foo6"=>27, 
             "foo7"=>11}
        }
    }
    require 'pp'
    pp sort_deep(hash)
    #=> {"en"=>
    #=>   {"key1"=>{"foo2"=>2, "foo1"=>1, "foo3"=>1},
    #=>    "key2"=>{"foo5"=>3, "foo6"=>2, "foo4"=>1, "foo7"=>1}},
    #=>  "fr"=>
    #=>   {"key1"=>{"foo1"=>91, "foo2"=>22, "foo3"=>12},
    #=>    "key2"=>{"foo5"=>31, "foo6"=>27, "foo4"=>21, "foo7"=>11}}}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Ruby hash that contains a user-generated string. I need to pass
I have a hash table that contains values of a^j. j is the key
I am currently learning Perl. I have Perl hash that contains references to hashes
I have a Perl class that contains a hash instance variable for storing other
If I have a hash in Perl that contains complete and sequential integer mappings
I have a legacy schema that contains tables with primary keys of type binary(16)
I have a hash (in Perl) where the values are all numbers. I need
I have a python hash that contains a mapping of function name to function.
I have a parameter hash that contains different variable and name pairs such as:
I have one folder that contains more number of xml files and extract some

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.