Notion formulas can sort arrays using the built-in sort() function, but the default behavior may not match your exact needs. By default, sort() arranges numbers in ascending order and text alphabetically. When you need a different order, such as sorting by a property value, date, or custom priority, you must supply a custom comparator function. This article explains how the comparator works, shows step-by-step examples for sorting by numbers, text, and dates, and covers common pitfalls to avoid.
Key Takeaways: Using sort() With a Custom Comparator in Notion
- sort(list, comparator): The second argument is a function that returns a negative, zero, or positive number to determine order.
- Comparator function signature:
compare(a, b)whereaandbare elements from the array. Return negative to placeabeforeb, positive for the reverse, or zero to keep the original order. - Sort by property: Use
a.propNameandb.propNameinside the comparator to sort objects by a specific field.
How the sort() Custom Comparator Works in Notion Formulas
The sort() function in Notion accepts an array and an optional comparator function. When you omit the comparator, Notion uses the default ascending order: numbers from smallest to largest, text alphabetically, and dates chronologically. The comparator function overrides this default behavior.
The comparator function receives two arguments, conventionally named a and b, which represent two elements from the array. It must return a number:
- Negative number (e.g., -1):
ashould come beforeb - Zero (0): the order of
aandbremains unchanged relative to each other - Positive number (e.g., 1):
bshould come beforea
For example, to sort numbers in descending order, you subtract b from a: sort([1, 3, 2], a - b) returns [3, 2, 1]. The comparator function can also access properties of objects, such as a.Name or b.Due Date, to sort by those fields. This is essential when your array contains rollup values, maps, or formula results that are structured as objects.
Steps to Apply a Custom Comparator With sort()
The following steps walk through building a formula that sorts an array of objects by a numeric property, then by text, and finally by date. All examples assume you have a database with a rollup that returns an array of related items, or you create an array manually using map() and filter().
Sort an Array of Numbers in Descending Order
- Create or identify the array
Suppose you have a rollup property named Scores that returns a list of numbers like[4, 9, 2, 7]. You want to sort from highest to lowest. - Write the formula with a comparator
In the formula property editor, enter:sort(prop("Scores"), a - b). Thea - bexpression returns a negative number whenais larger thanb, placing larger values first. - Verify the result
The formula outputs[9, 7, 4, 2]. If you see an error, ensure the property is a rollup of numbers and not a text string.
Sort an Array of Objects by a Numeric Property
- Understand the array structure
If you have a rollup that returns an array of pages, each page might contain a Priority number (1, 2, 3). The array elements are objects with aPriorityproperty. - Write the comparator using property access
Use:sort(prop("Related Tasks"), a.Priority - b.Priority). This sorts the tasks by priority in ascending order (1 first). - Sort in descending order by priority
Swap the subtraction:sort(prop("Related Tasks"), b.Priority - a.Priority)places higher priority numbers first.
Sort an Array of Objects by a Text Property Alphabetically
- Prepare the comparator for text
Notion does not have a built-in string comparison operator. Use theif()function to return -1, 0, or 1 based on alphabetical order. For ascending order (A to Z):sort(prop("Items"), if(a.Name < b.Name, -1, if(a.Name > b.Name, 1, 0))). - Apply the formula
ReplaceItemswith your rollup property name andNamewith the text property you want to sort by. The comparator compares the text values using the less-than and greater-than operators. - Reverse the order for descending (Z to A)
Swap the -1 and 1 values:sort(prop("Items"), if(a.Name > b.Name, -1, if(a.Name < b.Name, 1, 0))).
Sort an Array of Objects by a Date Property
- Understand date comparison
Dates in Notion formulas can be compared directly using the subtraction operator because dates are stored as timestamps. Subtracting one date from another returns the difference in milliseconds. - Sort by due date ascending (earliest first)
Use:sort(prop("Tasks"), a.Due Date - b.Due Date). This places the earliest due date at the top of the sorted array. - Sort by due date descending (latest first)
Reverse the subtraction:sort(prop("Tasks"), b.Due Date - a.Due Date).
Common Mistakes and Limitations When Using sort() With a Comparator
Comparator Returns a Boolean Instead of a Number
A common error is writing sort(list, a > b) which returns true or false. Notion converts boolean to 0 or 1, causing unstable sorting. Always return a negative, zero, or positive number. Use if(a > b, 1, if(a < b, -1, 0)) for text, or a - b for numbers.
The Array Contains Mixed Data Types
If your array mixes numbers and text, the comparator may produce unexpected results or an error. Ensure all elements in the array are of the same type before sorting. Use filter() to remove mismatched types, or convert them using toNumber() or format().
Property Names With Spaces or Special Characters
When accessing a property that contains a space, use bracket notation: a["Due Date"] instead of a.Due Date. Notion's formula parser treats a space as a syntax error. If the property name has special characters, bracket notation is required.
Comparator Does Not Handle Null or Empty Values
If a property is empty or undefined, the comparator may return NaN or cause the sort to fail. Add a check at the beginning of the comparator: if(empty(a.Property), 1, if(empty(b.Property), -1, ...)). This pushes empty values to the end.
| Item | Default sort() | Custom Comparator sort() |
|---|---|---|
| Sort order | Ascending only (numbers, text, dates) | Any order: ascending, descending, or by a specific property |
| Data type support | Numbers, text, dates | Numbers, text, dates, and objects with properties |
| Comparator syntax | None (omitted) | a - b, b - a, or if() block for text |
| Handles null values | No (may error) | Yes, with explicit empty() checks |
You can now sort any array in Notion formulas exactly the way you need. Start with a simple numeric comparator, then extend to text and date sorting. For complex arrays with mixed types, add empty checks and use bracket notation for property names with spaces. Practice by creating a test database with a rollup and applying a custom comparator to see the sorted output change in real time.