module Redis::Commands::Keys

def sort(key, by: nil, limit: nil, get: nil, order: nil, store: nil)

Returns:
  • (Array, Array>, Integer) -

Parameters:
  • options (Hash) --
  • key (String) --

Other tags:
    Example: Store an alphabetically descending list in "target" -
    Example: Retrieve the first 2 elements from an alphabetically sorted "list" -
def sort(key, by: nil, limit: nil, get: nil, order: nil, store: nil)
  args = [:sort, key]
  args << "BY" << by if by
  if limit
    args << "LIMIT"
    args.concat(limit)
  end
  get = Array(get)
  get.each do |item|
    args << "GET" << item
  end
  args.concat(order.split(" ")) if order
  args << "STORE" << store if store
  send_command(args) do |reply|
    if get.size > 1 && !store
      reply.each_slice(get.size).to_a if reply
    else
      reply
    end
  end
end