Module: DeepSort::ArrayExt

Included in:
Array
Defined in:
lib/deep_sort/array_ext.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

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



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

def self.deep_sort(array, options = {})
  array = array.map do |value|
    if value.is_a?(Array)
      DeepSort::ArrayExt.deep_sort(value, options)
    elsif value.is_a?(Hash)
      DeepSort::HashExt.deep_sort(value, options)
    elsif options[:sort_enum] and value.is_a?(Enumerable)
      DeepSort::ArrayExt.deep_sort(value.to_a, options)
    else
      value
    end
  end

  begin
    array.sort
  rescue
    array.sort_by(&:to_s)
  end
end

Instance Method Details

#deep_sort(options) ⇒ Object



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

def deep_sort(options)
  DeepSort::ArrayExt.deep_sort(self, options)
end

#deep_sort!(options = {}) ⇒ Object



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

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