How to Sort the Keys of a Hash in Ruby

0
3128

This guide walks through how to work with Ruby hashes, specifically how to sort through the keys by length.

Summary

Build a method that sorts the keys in a hash by length.

Exercise File

Code File

Exercise Description

Given a Ruby Hash, build a method that returns an array of keys that are sorted based on length.

Sample Input

{ some_key: 'Anything', "string key" => 'Anything', 8383 => 'Does not matter' }

Expected Output

["8383", "some_key", "string key"]

Real World Usage

Working with hashes in Ruby is critical when it comes to performing tasks such as working with database queries or any type of work specific to key value based data.

Solution

Can be found on the solutions branch on github.

LEAVE A REPLY

Please enter your comment!
Please enter your name here