Picture of the author

Notion Databases

Sorting

There are two possibilities to sort the results of a Notion base: Either sort by a property or a timestamp inside the database.

Sorting methods

If you want to sort by a property that is not a timestamp (but, for example, a number), use Sorting::propertySort("property name", "sorting order") to create a sorting object.

If the property is a timestamp, use Sorting::timestampSort("property name", "sorting order").

Add all sorting objects to a Collection and use the sortBy method of the endpoint to apply the sortings to the query. Instead of a Collection, you can also pass a single Sorting object if you only need one sorting property.

Sorting orders

The available sorting orders are ascending and descending.

Example

use Illuminate\Support\Collection;
use FiveamCode\LaravelNotionApi\Query\Sorting;

$sortings = new Collection();

# Sort the entries by birth year first, after that by creation timestamp
$sortings->add(Sorting::propertySort("Birth year", "ascending"));
$sortings->add(Sorting::timestampSort("created_time", "ascending"));

Notion::database("8284f3ff77e24d4a939d19459e4d6bdc")
    ->sortBy($sortings) // sorts are optional
    ->query()
    ->asCollection()
    ->each(function ($entry) {
        echo $entry->getTitle() . PHP_EOL;
    });


// Returns the entries sorted by birth year and created_time from our Test Database (https://www.notion.so/8284f3ff77e24d4a939d19459e4d6bdc) and prints their names (title property)
Previous
Filtering