# Filtering and Sorting

The Kendo UI for Vue Spreadsheet lets users sort the data of a sheet and show only the rows that match certain criteria.

The following example demonstrates a sheet with an applied filter and sort order. The filter hides the products priced below $10, and the rows are initially sorted by category. Use the buttons above the Spreadsheet to sort by price and to toggle the filter, or use the filter drop-down arrows in the header cells.

[Demo Source](https://demos.telerik.com/kendo-vue-ui/spreadsheet/filtering-sorting/source.txt)

## Using the Built-In Tools

By default, the **Data** tab of the [toolbar](https://www.telerik.com/kendo-vue-ui-develop/components/spreadsheet/toolbar) contains the **Filter** and **Sort** tools.

* **Filter**—Toggles the filter for the selected range. When the filter is enabled, the cells of the first row display a drop-down arrow. The filter menu lets users:
  * Sort the column in ascending or descending order.
  * Select the values that remain visible by using a searchable list with check boxes.
  * Filter by condition. The available conditions for text are `contains`, `does not contain`, `starts with`, `ends with`, `is equal to`, and `is not equal to`. For numbers, they are `is equal to`, `is not equal to`, `greater than`, `greater than or equal to`, `less than`, and `less than or equal to`. Two conditions can be combined with `And` or `Or`.
  * Clear the filter of the column.
* **Sort**—Sorts the whole sheet in ascending or descending order by the column of the active cell.

## Setting the Filter and the Sort Order in the Data

To initialize a sheet with a filter or a sort order, use the `filter` and `sort` fields of the sheet. The `ref` field is the range that is filtered or sorted, and the `index` field is the zero-based index of the column within that range.

```js
const sheets = [
    {
        name: 'Products',
        rows: [ /* ... */ ],
        filter: {
            ref: 'A1:D13',
            columns: [
                // Show only the rows with a price of at least 10.
                { index: 2, filter: 'custom', logic: 'and', criteria: [{ operator: 'gte', value: 10 }] },
                // Show only the rows with the listed values.
                { index: 1, filter: 'value', blanks: false, values: ['Beverages', 'Seafood'] }
            ]
        },
        sort: {
            ref: 'A2:D13',
            columns: [{ index: 1, ascending: true }]
        }
    }
];
```

The filter of a column is one of the following types:

| Type     | Description                                                                                                                                                |
| -------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `value`  | Shows the rows whose cells match one of the `values`. Set `blanks` to `true` to also show the empty cells.                                                 |
| `custom` | Shows the rows whose cells satisfy the `criteria`. Each criterion has an `operator` and a `value`. Use `logic` to combine the criteria with `and` or `or`. |

The supported `operator` values are `contains`, `doesnotcontain`, `startswith`, `endswith`, `eq`, `neq`, `gt`, `gte`, `lt`, and `lte`.

## Filtering and Sorting Programmatically

To change the filter or the sort order at runtime, execute the corresponding command through the `executeCommand` method of the Spreadsheet.

* Sort a range by a column:

  ```js
  const sheet = spreadsheet.value.activeSheet();

  spreadsheet.value.executeCommand({
      command: 'SortCommand',
      options: {
          value: 'desc', // 'asc' or 'desc'
          column: 2,
          operatingRange: sheet.range('A2:D13')
      }
  });
  ```

* Toggle the filter for a range:

  ```js
  spreadsheet.value.activeSheet().range('A1:D13').select();
  spreadsheet.value.executeCommand({ command: 'FilterCommand' });
  ```

To save the current filter and sort order, use the [`toJSON` or `saveJSON` method](https://www.telerik.com/kendo-vue-ui-develop/components/spreadsheet/methods). The returned sheets contain the `filter` and `sort` fields.

## Suggested Links

* [Toolbar](https://www.telerik.com/kendo-vue-ui-develop/components/spreadsheet/toolbar)
* [Data Validation](https://www.telerik.com/kendo-vue-ui-develop/components/spreadsheet/data-validation)
* [Methods](https://www.telerik.com/kendo-vue-ui-develop/components/spreadsheet/methods)
* [Globalization](https://www.telerik.com/kendo-vue-ui-develop/components/spreadsheet/globalization)
