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
  • 1 View
  • 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 list but I want to sort in a custom way
I have unsorted dictionary array and I want to sort it according to artist
How to sort list of date object ? For example I have unsorted list
I have a nested unsorted list and I want to place a click event
I have an unsorted UL list displaying search results. I want the user to
I have an unsorted list. I want each list item to display one or
I have to create a program that takes an input file of unsorted numbers
I have a list of userprofiles that I want to be able to sort
I have an dictonary for my input with the following characteristics: Each value will
I have an unsorted linked list. To sort it, I thought I'd put 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.