> ## Documentation Index
> Fetch the complete documentation index at: https://forest-chore-open-api.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Filtering & Sorting

## Filtering

### Disabling operators

<Info>
  Disable filtering without any code in the field settings in the Forest UI.
</Info>

### Substitution

Operation substitution serves two purposes:

* **Performance**: provide a more efficient way to perform a given filtering operation
* **Capabilities**: enable filtering on a computed field or other non-filterable fields

```javascript theme={null}
collection.replaceFieldOperator('fullName', 'Equal', (value, context) => {
  const [firstName, ...lastNames] = value.split(' ');

  return {
    aggregator: 'And',
    conditions: [
      { field: 'firstName', operator: 'Equal', value: firstName },
      { field: 'lastName', operator: 'Equal', value: lastNames.join(' ') },
    ],
  };
});
```

### Operators to support to enable search

| Column Type | Operator to support            |
| ----------- | ------------------------------ |
| Number      | Equal                          |
| Enum        | Equal                          |
| String      | IContains OR Contains OR Equal |
| Uuid        | Equal                          |

Use the `replaceFieldOperator` method to unlock the operators.

### Emulation

Filtering emulation allows making fields filterable automatically.
It is a convenient way to get things working quickly for collections that have a low number of records (in the thousands at most).

<Warning>
  This emulation forces the back-end to retrieve all the collection records and compute the field values for each one of them.
  As a consequence, filtering emulation performance cost is **linear** with the number of records in the collection, so **activate it sparingly and with great care**.
</Warning>

```javascript theme={null}
// Add support for all operators
collection.emulateFieldFiltering('fullName');

// Add support for a single operator
collection.emulateFieldOperator('fullName', 'Equal');
```

***

## Sorting

Depending on the data source, not all fields may be sortable, or you may want to change how the native sorting works.

Use the `replaceFieldSorting` and `emulateFieldSorting` methods to change a single column's sorting behavior.

### Substitution

Provide replacement sort clauses. In this example, we're telling Forest "When a user sorts by full name, I want to sort by the last name, and then by the first name".

```javascript theme={null}
collection.replaceFieldSorting('fullName', [
  { field: 'lastName', ascending: true },
  { field: 'firstName', ascending: true },
]);
```

Another very common reason is performance. For instance, with auto-incrementing ids, sorting by `creationDate` is equivalent to sorting by the primary key in reverse order.

Using sort substitution where needed can save you from adding many indexes to your database.

```javascript theme={null}
// Sorting by creationDate ascending <=> Sorting by id descending
collection.replaceFieldSorting('creationDate', [{ field: 'id', ascending: false }]);
```

### Emulation

Sorting emulation allows making any field automatically sortable. It will sort records by lexicographical order.
It is a convenient way to get things working quickly for collections that have a low number of records (in the thousands at most).

<Warning>
  This emulation forces the back-end to retrieve all the collection records and compute the field values for each one of them.
  As a consequence, sorting emulation performance cost is **linear** with the number of records in the collection, so **activate it sparingly and with great care**.
</Warning>

```javascript theme={null}
collection.emulateFieldSorting('fullName');
```
