Sha256: b4345d7dc9ed53b5505b5061cd3a223dd37ada43e2e042230a425df15c129366

Contents?: true

Size: 581 Bytes

Versions: 25

Compression:

Stored size: 581 Bytes

Contents

module JSONAPIonify
  module DeepSortCollection
    refine Array do
      def deep_sort(hash)
        keys   = hash.to_a
        sorter = lambda do |iterator, depth = 0|
          key_name, order = keys[depth]
          if key_name
            sorted = iterator.sort_by(&key_name)
            sorted.reverse! if order == :desc
            sorted.group_by(&key_name).values.map do |value|
              sorter.call(value, depth + 1)
            end.reduce(:+) || []
          else
            iterator
          end
        end
        sorter.call(self)
      end
    end
  end
end

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
jsonapionify-0.12.10 lib/jsonapionify/deep_sort_collection.rb
jsonapionify-0.12.9 lib/jsonapionify/deep_sort_collection.rb
jsonapionify-0.12.8 lib/jsonapionify/deep_sort_collection.rb
jsonapionify-0.12.7 lib/jsonapionify/deep_sort_collection.rb
jsonapionify-0.12.5 lib/jsonapionify/deep_sort_collection.rb
jsonapionify-0.12.4 lib/jsonapionify/deep_sort_collection.rb
jsonapionify-0.12.3 lib/jsonapionify/deep_sort_collection.rb
jsonapionify-0.12.2 lib/jsonapionify/deep_sort_collection.rb
jsonapionify-0.12.1 lib/jsonapionify/deep_sort_collection.rb
jsonapionify-0.12.0 lib/jsonapionify/deep_sort_collection.rb
jsonapionify-0.11.11 lib/jsonapionify/deep_sort_collection.rb
jsonapionify-0.11.10 lib/jsonapionify/deep_sort_collection.rb
jsonapionify-0.11.9 lib/jsonapionify/deep_sort_collection.rb
jsonapionify-0.11.8 lib/jsonapionify/deep_sort_collection.rb
jsonapionify-0.11.7 lib/jsonapionify/deep_sort_collection.rb
jsonapionify-0.11.6 lib/jsonapionify/deep_sort_collection.rb
jsonapionify-0.11.5 lib/jsonapionify/deep_sort_collection.rb
jsonapionify-0.11.0 lib/jsonapionify/deep_sort_collection.rb
jsonapionify-0.10.2 lib/jsonapionify/deep_sort_collection.rb
jsonapionify-0.10.1 lib/jsonapionify/deep_sort_collection.rb