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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T23:29:06+00:00 2026-05-10T23:29:06+00:00

I have unsorted map of key value pairs. input = { xa => xavalue,

  • 0

I have unsorted map of key value pairs.

input = {   'xa' => 'xavalue',   'ab' => 'abvalue',   'aa' => 'aavalue',   'ba' => 'bavalue', } 

Now I want to sort them by the key and cluster them into sections by the first character of the key. Similar to this:

output1 = {   'a' => {       'aa' => 'aavalue',       'ab' => 'abvalue',     },   'b' => {     'ba' => 'bavalue',   },     'x' => {     'xa' => 'xavalue',   },   } 
  1. While this is relatively trivial I am looking for a concise way to express this transformation from input to output1 in ruby. (My approach is probably too verbose as to ruby standards)

  2. You might also have noticed that maps are (usually) not ordered. So the above data structure will not work appropriately unless I manually sort the keys and wrap the access to the map. So how would I create a key ordered map in ruby? Or is there one already?

  3. If the ordered map approach is not that easy I would have to change the final structure into something like the following. Again I am looking for some concise ruby code to come from input to output2.

.

output2 = [   {     'name' => 'a',     'keys' => [ 'aa', 'ab' ],     'values' => [ 'aavalue', 'abvalue' ],   },   {     'name' => 'b',     'keys' => [ 'ba' ],     'values' => [ 'bavalue' ],   },   {     'name' => 'x',     'keys' => [ 'xa' ],     'values' => [ 'xavalue' ],   } ] 
  • 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. 2026-05-10T23:29:07+00:00Added an answer on May 10, 2026 at 11:29 pm

    As far as I know, there isn’t the notion of a sorted hash/map in Ruby, so you’re limited to good old arrays for this one. You may want to start from this code:

    output = input.inject({}) { |acc, pair|   letter = pair.first[0].chr   acc[letter] ||= {}   acc[letter][pair.first] = pair.last   acc }.sort 

    This will give you a data structure in the form

    [ ['a', {'aa'=>'aavalue',          'ab'=>'abvalue'}],   ['b', {'ba'=>'bavalue'}],   ['x', {'xa'=>'xavalue'}]] 

    By mapping the component arrays into hashes…

    output.map {|pair| {pair.first => pair.last}} 

    You can transform it into something like

    [{'a'=>{'aa'=>'aavalue',         'ab'=>'abvalue'}},  {'b'=>{'ba'=>'bavalue'}},  {'x'=>{'xa'=>'xavalue'}}] 

    With a similar map, you can get to the last form you listed (output2):

    output2 = output.map { |pair|   hash = pair.last   { 'name' => pair.first,     'keys' => hash.keys,     'values' => hash.values } } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an unsorted linked list. To sort it, I thought I'd put the
I have two unsorted lists and I need to produce another list which is
I have an unsorted list of noisy X, Y points. They do, however, form
I have unsorted set of line segments without any binding to each other. Each
I have an unsorted Array holding the following IDs: @un_array = ['bar', 'para-3', 'para-2',
Have just started using Google Chrome , and noticed in parts of our site,
Have you ever seen any of there error messages? -- SQL Server 2000 Could
Have you guys had any experiences (positive or negative) by placing your source code/solution
Have just started using Visual Studio Professional's built-in unit testing features, which as I
Have you used VS.NET Architect Edition's Application and System diagrams to start designing a

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.