Deeply Sort Nested Ruby Arrays and Hashes

Mark Crossen

Reading time: about 2 min

Topics:

  • Behind The Scenes
  • Open Source

If you’ve ever scripted in Ruby before, you’ve more than likely encountered deeply nested arrays and hashes. These nested structures often come from Ruby’s JSON parser, but Ruby itself doesn’t have effective methods for dealing with them. Specifically, sorting these structures: the standard routine only shallow sorts the top layer instead of deeply sorting every nested array and hash that the structure contains. This issue became apparent in Cumulus—an open source project sponsored by Lucid Software. After being unable to find an adequate solution, a simple utility was made that deeply sorts nested structures. Although small, the utility was incredibly useful—so it was made into a standalone Ruby Gem.

For instance, say your project has a big nasty JSON file that needs to be parsed and recursively sorted:
[
  {
    "name": "Steve",
    "relatives": [
      "Ray",
      "Mark",
      "Jeff",
      "David"
    ]
  },
  {
    "relatives": [
      "Mary",
      "Joe",
      "Harry"
    ],
    "name": "John"
  }
]
The deepsort gem makes sorting this nested array-of-hashes-of-arrays trivial. Simply parse the JSON into a Ruby structure, and call the “deep_sort” method on it:
require “deepsort”
require “json”
result = JSON.parse(File.read("nasty.json")).deep_sort
puts JSON.pretty_generate(result)
Doing so returns a cleanly sorted structure:
[
  {
    "name": "John",
    "relatives": [
      "Harry",
      "Joe",
      "Mary"
    ]
  },
  {
    "name": "Steve",
    "relatives": [
      "David",
      "Jeff",
      "Mark",
      "Ray"
    ]
  }
]

But wait—the goodness doesn't stop there! The gem also includes in-place and configurable methods such as deep_sort!, deep_sort_by, and deep_sort_by!, similar to ruby's built-in sort!, sort_by, and sort_by!

To install deepsort, either include it in a project's bundled dependencies (when using a utility like bundler), or use the gem utility that comes with Ruby:
gem install deepsort

The deepsort gem is Open Source (MIT license). Feel free to contribute, comment, or learn more on the project's GitHub page.

About Lucid

Lucid Software is a pioneer and leader in visual collaboration dedicated to helping teams build the future. With its products—Lucidchart, Lucidspark, and Lucidscale—teams are supported from ideation to execution and are empowered to align around a shared vision, clarify complexity, and collaborate visually, no matter where they are. Lucid is proud to serve top businesses around the world, including customers such as Google, GE, and NBC Universal, and 99% of the Fortune 500. Lucid partners with industry leaders, including Google, Atlassian, and Microsoft. Since its founding, Lucid has received numerous awards for its products, business, and workplace culture. For more information, visit lucid.co.

Get Started

  • Contact Sales

Products

  • Lucidspark
  • Lucidchart
  • Lucidscale
PrivacyLegalCookie privacy choicesCookie policy
  • linkedin
  • twitter
  • instagram
  • facebook
  • youtube
  • glassdoor
  • tiktok

© 2024 Lucid Software Inc.