class Realize::Collection::Sort
Transformer to take an array of objects and sort by the given key and by the given sort direction. Defaulting to ascending.
Constants
- DEFAULT_ORDER
Attributes
key[R]
order[R]
Public Class Methods
new(key:, order: DEFAULT_ORDER)
click to toggle source
# File lib/realize/collection/sort.rb, line 25 def initialize(key:, order: DEFAULT_ORDER) raise ArgumentError, 'key is required' if key.to_s.empty? @key = key @order = Direction.const_get(order.to_s.upcase.to_sym) freeze end
Public Instance Methods
transform(resolver, value, _time, _record)
click to toggle source
# File lib/realize/collection/sort.rb, line 34 def transform(resolver, value, _time, _record) records = array(value) sorted = records.sort_by { |hsh| resolver.get(hsh, key) } order == DESCENDING ? sorted.reverse : sorted end