Module: HashHelper::DeepSort
- Included in:
- Hash
- Defined in:
- lib/hash_helper/deep_sort.rb
Instance Method Summary collapse
-
#deep_sort ⇒ Hash
Recursively sorts the hash by keys in lexicographic order.
-
#deep_sort_by {|key, value| ... } ⇒ Hash
Recursively sorts the hash by a custom block.
Instance Method Details
#deep_sort ⇒ Hash
Recursively sorts the hash by keys in lexicographic order.
14 15 16 |
# File 'lib/hash_helper/deep_sort.rb', line 14 def deep_sort deep_sort_by { |key, _value| key } end |
#deep_sort_by {|key, value| ... } ⇒ Hash
Recursively sorts the hash by a custom block.
35 36 37 38 39 40 41 42 43 |
# File 'lib/hash_helper/deep_sort.rb', line 35 def deep_sort_by(&block) sorted_pairs = sort_by do |key, value| block.call(key, value.is_a?(Hash) ? nil : value) end sorted_pairs.each_with_object({}) do |(key, value), result| result[key] = value.is_a?(Hash) ? value.deep_sort_by(&block) : value end end |