Sorting through complex Hashes

Andrew Smoker
3 min readJul 9, 2021

Recently I was given a take home technical assessment where the task at hand was sorting a hash based on certain parameters. The assessment had to be done in Ruby so I was able to refresh my iterating through objects skills. I’m going to walk through how I ended up at a solution by using a similar example.

Let’s say the original hash looks like this:

people = {:fredy => { :name => "Fred", :age => [23, 25, 3] }, :joanne => { :name => "Joan", :age => [18, 19, 2] }, :pete => { :name => "Pete", :age => [54, 2] }, :ann => {:name => "Anne"}
}

The people hash consists of three keys which are also hashes with their own keys and values. The age key points us to an array of ages, not just one number. The task at hand was to sort the people hash so that the ages were sorted in order. This seems easy because we can call .sort on an array and get our sorted ages! But how do we keep that value assigned to our key in the hash?

I started by iterating through the hash’s key value pairs. This gave me access to :fredy as the key and { :name => “Fred”, :age => [23, 25, 3] } as the value. Then, if the value had an “age” key, I could reset it by assigning it a sorted version of the ages. This goes through each of the main hash’s keys, checks if the object has an age key and if it does, reassigns that key to a sorted version of the array.

puts people.each{ |k, v| v[:age] = v[:age].sort if v[:age]}

This returns the following hash. As you can see the people hash is still returned in the same order (fredy, joanne, pete and ann), but now the age array is sorted from lowest to highest! Notice that for :ann nothing changed because there was no age array and the block of code never ran.

{:fredy=>{:name=>"Fred", :age=>[3, 23, 25]}, :joanne=>{:name=>"Joan", :age=>[2, 18, 19]}, :pete=>{:name=>"Pete", :age=>[2, 54]}, :ann=>{:name=>"Anne"}}

Now that we have our ages sorted, let’s say we also wanted to sort the people hash by the length of the symbols (fredy, joanne, pete, ann). To do this we can use Ruby’s sort_by method that gives us a key, value pair to work with. In this case we want to access the length of our key after turning it to a string. This normally returns an array so we have to add .to_h at the end to return our hash.

.sort_by{|k, v|k.to_s.length}.to_h

By adding that on behind our previous code we get the following result. You can see that now our hash is sorted by the length of its keys AND the ages arrays are sorted in order!

{:ann=>{:name=>"Anne"}, :pete=>{:name=>"Pete", :age=>[2, 54]}, :fredy=>{:name=>"Fred", :age=>[3, 23, 25]}, :joanne=>{:name=>"Joan", :age=>[2, 18, 19]}}

Working with hashes is incredibly important because we access data from them all the time using different APIs. Knowing how to access certain elements in a hash and sort or extract the data needed becomes very useful. This is still a small example, but gives some helpful tips to use when dealing with complex hashes. I would love to hear any feedback on other methods of solving this problem and sorting through hashes, so please feel free to comment!

--

--

Andrew Smoker

I am 34 years old and making a huge career change by attending Flatiron School’s Software Engineering Bootcamp. Excited to learn!