class Hash

This reopens the Hash class to add a method to allow sorting by key recursively

Public Instance Methods

sort_by_key(recursive = false, &block) click to toggle source
# File lib/bookpress.rb, line 10
def sort_by_key(recursive = false, &block)
  self.keys.sort(&block).reduce({}) do |seed, key|
    seed[key] = self[key]
    if recursive && seed[key].is_a?(Hash)
      seed[key] = seed[key].sort_by_key(true, &block)
    end
    seed
  end
end