When Notion shipped Formulas 2.0, it included support for Lists (or Arrays in programming parlance). One of the functions included for working with lists is the sort
function. Unlike JavaScript’s sort
function, Notion’s sort
initially only sorted the list in a very specific way.
[3, 1, 2].sort() /* => [1, 2, 3] */
The rules for sorting in Notion are a bit complex, but here’s the general gist (note that this definitely differs from how things are sorted in JavaScript!):
Since by default the lists were sorted by the above rules, this meant that in order to sort a list in the opposite direction, we had to do something like this:
[3, 1, 2].sort().reverse() /* => [3, 2, 1] */
As of early December, you can now use an expression passed to sort
to modify your sorting. In this case, sort
receives what’s referred to as the “current context”. The code inside the ()
will be run on each item in the list, meaning that current
will be first 3, then 1, and finally, 2.
[3, 1, 2].sort(current * -1) /* [3, 2, 1] */
In the above example, we’re multiplying each item in the list by -1
, and that value is what is used to sort the original list. In effect, this means that the resulting list is sorted in descending order.