Skip to content
Daniel Gorman edited this page May 21, 2019 · 3 revisions

The SORT Function

Function Group: Collection

SORT sorts a collection by the values returned from applying a sorting function to each element in said collection.

Syntax

SORT(arg1, arg2)

  • arg1 is a collection
  • arg2 is the sorting function that will be applied to each element of the collection

Uses

Let's say we're given a response with some vehicle information that looks like this:

{  
   "data":{  
      "fleet_prices":[16000, 17450, 9200],
      "fleet_names": ["BMW", "Audi", "Mercedes Benz"]
    }
}

If we want to organize those prices by basic numerical value, we can use SORT with the dynamic references themselves serving as a sorting function:

SORT(data.fleet_prices, _)

This would return [9200, 16000, 17450].

Other examples:

We can also sort strings with that technique, as shown below:

SORT(data.fleet_names, _) => ["Audi", "BMW", "Mercedes Benz"]

We can also apply a function to the dynamic reference, as so:

SORT(data.fleet_names, LEN(_)) => ["Mercedes Benz", "Audi", "BMW",]

Clone this wiki locally