Sorting

This is the official documentation of Forest Admin Cloud.

Emulation

To quickly make a field sortable, you can use sorting emulation for lexicographical order. This method works best for collections with a few thousand records or less. It's a straightforward way to get started without much setup.


To make the code easier to read, all the code snippets below should be wrapped in the following code. Ensure you update the collection and field names as needed.

import type { Agent } from '@forestadmin/forest-cloud';
import { Schema } from '../typings';

export default function customizeAgent(agent: Agent<Schema>) {
  agent.customizeCollection('users', (collection) => {
    // Insert the code snippet here.
  });
}

Enabling sorting emulation

collection.emulateFieldSorting('fullName');

Using fields emulation can impact the performance of your admin panel. It is crucial to review best practices for fields to guarantee an optimal experience for your admin panel users.

Overriding sorting

With replaceFieldSorting(), you can change how records are sorted. For instance, if someone wants to sort by full name, it will sort by last name first, then by first name.

collection.replaceFieldSorting('fullName', [
  { field: 'lastName', ascending: true },
  { field: 'firstName', ascending: true },
]);

Another reason for customizing the sort is to optimize performance. For instance, sorting by creationDate can be achieved by sorting the primary key in reverse order when using auto-incrementing ids. This helps avoid adding multiple indexes and improves database performance.

collection.replaceFieldSorting('creationDate', [{ field: 'id', ascending: false }]);

Last updated