Module: DeepSort::Hash

Included in:
Hash
Defined in:
lib/deep_sort/hash.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.deep_sort(hash, options = {}) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/deep_sort/hash.rb', line 11

def self.deep_sort(hash, options = {})
  new_hash = {}

  hash.keys.sort_by(&:to_s).each do |key|
    value = hash[key]

    case value
    when Array, Hash
      value = DeepSort.deep_sort(value, options)
    when Enumerable
      if options[:sort_enum]
        value = DeepSort.deep_sort(value.to_a, options)
      end
    end

    new_hash[key] = value
  end

  new_hash
end

Instance Method Details

#deep_sort(options = {}) ⇒ Object



7
8
9
# File 'lib/deep_sort/hash.rb', line 7

def deep_sort(options = {})
  DeepSort::Hash.deep_sort(self, options)
end

#deep_sort!(options = {}) ⇒ Object



2
3
4
5
# File 'lib/deep_sort/hash.rb', line 2

def deep_sort!(options = {})
  new_hash = self.deep_sort(options)
  self.replace(new_hash)
end