List Sort By Key | ARRAY_SORTBY

Syntax

ARRAY_SORTBY( [value], [key], [direction], [secondaryKey], [secondaryDirection] )

Parameters

value
An ARRAY to sort. Should be an ARRAY of MAPS / OBJECTS.

key
A path into each item to use for sorting. For more information on the path syntax used by instalink.io see the dedicated documentation on that subject.

direction
A sort direction as a number. A negative number sorts in descending order, a positive number sorts in ascending order.

secondaryKey
A path into each item to use for sorting when the paths at the key are equal. For more information on the path syntax used by instalink.io see the dedicated documentation on that subject.

secondaryDirection
A sort direction as a number for the secondaryKey when used. A negative number sorts in descending order, a positive number sorts in ascending order.

Return Value

Returns an ARRAY of items in VALUE that have been sorted by value at KEY.

Description

Sort items in an ARRAY based on a path within each item.

Examples

ARRAY_SORTBY([], "a.b", 1) # returns []
# VALUE is [["a" => 1], ["a" => 3], ["a"=> 2]
ARRAY_SORTBY(VALUE, "a", 1) # returns [["a" => 1], ["a" => 2], ["a" => 3]]
# VALUE is [["a" => 1], ["a" => 3], ["a"=> 2]
ARRAY_SORTBY(VALUE, "a", -1) # returns [["a" => 3], ["a" => 2], ["a" => 1]]
# VALUE is [["a" => ["b" => 1]], ["a" => ["b" => 3], ["a" => ["b" => 2]]]
ARRAY_SORTBY(VALUE, "a.b", 1) # returns [["a" => ["b" => 1]], ["a" => ["b" => 2], ["a" => ["b" => 3]]]
# VALUE is [["a" => 1, "b" => 2], ["a" => 2, "b" => 1], ["a" => 1, "b" => 1]]
ARRAY_SORTBY(VALUE, "a", 1, "b", 1) # returns [["a" => 1, "b" => 1], ["a" => 1, "b" => 2], ["a" => 2, "b" => 1]]
# VALUE is [["a" => 1, "b" => 2], ["a" => 2, "b" => 1], ["a" => 1, "b" => 1]]
ARRAY_SORTBY(VALUE, "a", 1, "b", -1) # returns [["a" => 1, "b" => 2], ["a" => 1, "b" => 1], ["a" => 2, "b" => 1]]
ARRAY_SORTBY(null, "a.b") # returns []
# VALUE is [["a" => 1, "b" => 2], ["a" => 3, "b" => 4], ["a"=> 1, "b" => 5]]
ARRAY_SORTBY(VALUE, null) # returns []