SORT
sorts a collection by the values returned from applying a sorting function to each element in the collection.
Function category: Collection​
SORT(arg1, arg2)
Argument | Description |
arg1 | A key function. Each element of the collection is passed to that function and then sorted based on the function's result. An underscore ( _ ) represents a placeholder, which is the value itself (dynamic reference). |
arg2 | A collection to be sorted. |
Let's say we're given a response with the following vehicle information:
{"data":{"fleet_prices": [16000, 17450, 9200],"fleet_names": ["BMW", "Audi", "Mercedes Benz"]}}
If we want to organize these prices by basic numerical value, we can use SORT
with the dynamic references themselves serving as a sorting function.
# Sort prices using dynamic referencesSORT(_, data.fleet_prices)​# Returns [9200, 16000, 17450]
We can also sort strings using SORT
with dynamic references.
# Sort strings using dynamic referencesSORT(_, data.fleet_names)​# Returns ["Audi", "BMW", "Mercedes Benz"]
We can also apply a function such as LEN to the dynamic reference. This sorts by length of string.
# Sort strings using the LEN function with dynamic referencesSORT(LEN(_), data.fleet_names)​# Returns ["Mercedes Benz", "Audi", "BMW",]