class JMESPath::Nodes::SortByFunction

Public Instance Methods

call(args) click to toggle source
# File lib/jmespath/nodes/function.rb, line 450
def call(args)
  if args.count == 2
    if get_type(args[0]) == ARRAY_TYPE && get_type(args[1]) == EXPRESSION_TYPE
      values = args[0]
      expression = args[1]
      values.sort do |a,b|
        a_value = expression.eval(a)
        b_value = expression.eval(b)
        a_type = get_type(a_value)
        b_type = get_type(b_value)
        if (a_type == STRING_TYPE || a_type == NUMBER_TYPE) && a_type == b_type
          a_value <=> b_value
        else
          return maybe_raise Errors::InvalidTypeError, "function sort() expects values to be an array of numbers or integers"
        end
      end
    else
      return maybe_raise Errors::InvalidTypeError, "function sort_by() expects an array and an expression"
    end
  else
    return maybe_raise Errors::InvalidArityError, "function sort_by() expects two arguments"
  end
end