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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T01:12:51+00:00 2026-06-05T01:12:51+00:00

How would I reverse the elements in the hash, keeping the same values and

  • 0

How would I reverse the elements in the hash, keeping the same values and keys, but reversing their order in the hash.

Like so:

{ "4" => "happiness", "10" => "cool", "lala" => "54", "1" => "spider" }

And convert that to:

{ "1" => "spider", "lala" => "54", "10" => "cool", "4" => "happiness" }

Or, perhaps I could run a each loop backwards, starting from the last element in the hash, rather than the first?

  • 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-06-05T01:12:53+00:00Added an answer on June 5, 2026 at 1:12 am

    You could convert the Hash to an Array, reverse that, and then convert it back to a Hash:

    reversed_h = Hash[h.to_a.reverse]
    

    Hash#to_a gives you an array of arrays, the inner arrays are simple [key,value] pairs, then you reverse that array using Array#reverse, and Hash[] converts the [key,value] pairs back into a Hash.

    Ruby 2.1 adds an Array#to_h method so you can now say:

    reversed_h = h.to_a.reverse.to_h
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I asked the question here and now i would like to reverse this procedure
I have an application that I have behind a reverse proxy, I would like
I would like to show 10 elements (images) incrementally. When the 9th image is
I would like to use the following code in C# but I just can't
i'm new to jquery.I'd like to know how to iterate form elements in reverse
I have a ArrayList, with elements something like: [string,has,was,hctam,gnirts,saw,match,sah] I would like to delete
I have a pretty specific scenario where I would like to select all elements
How would I do in a SELECT query to reverse this path : z/y/x
Would like to sort the following XML by joining the fsxml/formula/ingrrow where itemcode =
I'm working with the DataContractJsonSerializer in Silverlight 4 and would like to deserialize the

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.