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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:00:59+00:00 2026-05-26T15:00:59+00:00

Can you please help anyone?. I have a hash value as following. I need

  • 0

Can you please help anyone?. I have a hash value as following. I need to sort the hash in reverse order by key value and return as also hash type.

My actual expected output is (hash)

result = {20111104111221=>[4, 5, 6], 20111104111220=>[7, 8, 9], 20111104110950=>[1, 2, 3]}
# this is my input
irb(main):096:0> h = {20111104111221=>[4, 5, 6], 20111104110950=>[1, 2, 3], 20111104111220=>[7, 8, 9]} 
=> {20111104111221=>[4, 5, 6], 20111104110950=>[1, 2, 3], 20111104111220=>[7, 8, 9]}

# It's not correct
irb(main):095:0> Hash[h.sort]
=> {20111104111221=>[4, 5, 6], 20111104110950=>[1, 2, 3], 20111104111220=>[7, 8, 9]}

So, i tried this. It’s correct but it’s return as a array value, i need a return value as Hash.

# It's correct but it's not a hash
irb(main):092:0> arr = h.sort_by { |k,v| k }.reverse
=> [[20111104111221, [4, 5, 6]], [20111104111220, [7, 8, 9]], [20111104110950, [1, 2, 3]]]

Again i tried array to hash conversion.. but it’s help.

# It's also not correct.
irb(main):092:0> irb(main):098:0> Hash[*arr.flatten]
=> {5=>6, 20111104111221=>4, 20111104110950=>1, 2=>3, 8=>9, 20111104111220=>7}
  • 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-26T15:01:00+00:00Added an answer on May 26, 2026 at 3:01 pm

    Which version of ruby are you using? In 1.8 hashes cannot be ordered, in 1.9 they are ordered based on insertion. Here’s more info on that.

    Given the variation in how they’re handled I wouldn’t focus on sorting the hash itself, but on sorting your keys and using them as a reference, something like:

    data = {20111104111221=>[4, 5, 6], 20111104111220=>[7, 8, 9], 20111104110950=>[1, 2, 3]}
    
    keys = data.keys.sort.reverse
    keys.each do |key|
      puts data[key].pretty_inspect
      # ... do work ...
    end
    

    The debug line is in there just as an example of how to access your values. Hope this helps!

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

Sidebar

Related Questions

Can anyone please help. I'm following a tutorial found here as I have a
Can anyone please help. I have a problem where I need to loop through
Can anyone please help me on the following: I have created a SQL Server
Can anyone please help me how to convert string to date I have 3
Can anyone please help me to work out how to achieve the following? I
Can anyone please help me with this? I have a Windows 2008 server and
Can anyone please help me by solving problem in my application.I have three input
Can anyone please help me on below? I have a WSDL and some XSD
Can anyone please help in finding the solution of the above question I have
can anyone please help me with this query. I have a CSV file which

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.