# Sorting and Filtering

> Sort ranges, set AutoFilter criteria and preview text-to-columns, duplicate removal and consolidation with sortRange, setAutoFilter and previewRemoveDuplicates.

- **Product:** TX Sheet Control
- **Section:** Documentation
- **Kind:** Topic
- **Path:** TX Sheet Control > Spreadsheet Documents > Sorting and Filtering
- **1543 words**
- **Web URL:** https://docs.textcontrol.com/tx-sheet-control/spreadsheets/sorting-filtering/
- **LLMs URL:** https://docs.textcontrol.com/tx-sheet-control/spreadsheets/sorting-filtering/llms.txt
- **LLMs-Full URL:** https://docs.textcontrol.com/tx-sheet-control/spreadsheets/sorting-filtering/llms-full.txt

---

Sorting and AutoFilter operate on the data of a worksheet. Beside them, the **Data** tab offers transformations that split text into columns, remove duplicate rows and consolidate ranges, each with a preview. Dashboard filters are a different mechanism. They operate on analytics and do not rewrite worksheet values or AutoFilter settings.

### Sort and Filter on the Data Tab

Select a rectangular data range with intentional headers. Then use the commands of the **Data** tab, shown in [Ribbon and Contextual Commands](https://docs.textcontrol.com/tx-sheet-control/spreadsheet-editor/ribbon/llms-full.txt), to:

- sort by supported keys
- open filter menus and clear criteria
- split text into columns
- remove duplicate rows
- consolidate supported ranges of the same worksheet

In **Sort**, use **Add Level** to sort by another column and order. In **Advanced Filter**, use **Add Condition** to add a criterion. The **×** button beside each row removes that level or condition; the dialog retains at least one row.

### Split Text Into Columns

Select the source cells and choose **Data → Text to Columns**. The dialog separates **Data & Destination**, **Split Settings** and **Output** into exclusive accordion sections. Choose delimited text or fixed-width positions, configure the split and choose the output format. **Text (Keep Leading Zeros)** is useful for identifiers that must remain text.

![Text to Columns with delimiter settings and a right Live Preview of two destination columns](https://docs.textcontrol.com/assets/img/tx-sheet-control/text-to-columns-dialog.f413ee81.webp)

Inspect the right **Live Preview**, destination and replacement count before choosing **Finish**. Enable replacement only when the existing destination cells may be overwritten. **Cancel** discards the draft.

### Remove Duplicate Records

Select the range and choose **Data → Remove Duplicates**. In **Source Data**, check the range and whether it has headers. In **Comparison**, choose the columns to compare and whether matching is case-sensitive. The preview lists retained records and reports the duplicate and unique row counts.

![Remove Duplicates with comparison-column switches and a right preview of retained records and row counts](https://docs.textcontrol.com/assets/img/tx-sheet-control/remove-duplicates-dialog.4cbe13e6.webp)

Choose **Remove Duplicates** to keep the first matching record and compact the selected range. Check the comparison columns before applying: rows with different values outside those columns can still count as duplicates. Use **Cancel** to leave the data unchanged.

### Consolidate Worksheet Ranges

Choose **Data → Consolidate** and enter the source ranges and operation in **Source Data**. Set the destination and replacement policy in **Output**. In **Labels & Links**, choose whether the top row and left column supply labels and whether the output creates links to source data.

![Consolidate with label and link switches beside a preview of the consolidated values and destination](https://docs.textcontrol.com/assets/img/tx-sheet-control/consolidate-dialog.84ab1ae1.webp)

Check the result and destination in **Live Preview**, then choose **Consolidate** to apply or **Cancel** to discard the draft. All three transformation dialogs use the same fixed settings frame: opening a section or changing an option keeps the header, preview and actions in place while longer content scrolls internally.

### Sort and Filter in Code

The workbook API exposes four methods for sorting and AutoFilter:

| Method | Purpose |
|---|---|
| [sortRange](https://docs.textcontrol.com/tx-sheet-control/reference/javascript-api/browser/workbook/sortrange-method/llms-full.txt) | Sorts a range of the active sheet in one undoable operation. Rejects merged ranges and invalid keys. |
| [setAutoFilter](https://docs.textcontrol.com/tx-sheet-control/reference/javascript-api/browser/workbook/setautofilter-method/llms-full.txt) | Sets or removes the filter of the active sheet. |
| [getAutoFilter](https://docs.textcontrol.com/tx-sheet-control/reference/javascript-api/browser/workbook/getautofilter-method/llms-full.txt) | Returns the filter of the active sheet as a detached snapshot. |
| [clearFilters](https://docs.textcontrol.com/tx-sheet-control/reference/javascript-api/browser/workbook/clearfilters-method/llms-full.txt) | Removes the filter criteria and keeps the filter range. |

Sort options and filter criteria are typed definitions. Use the reference, such as [SortOptions](https://docs.textcontrol.com/tx-sheet-control/reference/typescript-api/browser/sortoptions/llms-full.txt), rather than constructing undocumented criterion objects. Merged ranges and protected targets can reject transformations.

### Preview a Transformation Before You Apply It

Text to columns, duplicate removal and consolidation provide preview operations: [previewTextToColumns](https://docs.textcontrol.com/tx-sheet-control/reference/javascript-api/browser/workbook/previewtexttocolumns-method/llms-full.txt), [previewRemoveDuplicates](https://docs.textcontrol.com/tx-sheet-control/reference/javascript-api/browser/workbook/previewremoveduplicates-method/llms-full.txt) and [previewConsolidate](https://docs.textcontrol.com/tx-sheet-control/reference/javascript-api/browser/workbook/previewconsolidate-method/llms-full.txt). Inspect the preview and the destination before you apply the operation.

- **Text to columns.** A text split can require overwrite permission if the target cells already contain data.
- **Duplicate removal.** It compares the configured columns and the header policy, not merely the visible formatting.

The following code previews which rows a duplicate removal in `A1:D100` would keep and remove:

```javascript
const book = editor.workbook;
const preview = book.previewRemoveDuplicates('A1:D100', { hasHeaders: true });
console.log(preview);
// Confirm the intended keys/options before calling removeDuplicates.
```

The code works as follows:

- [Workbook.previewRemoveDuplicates](https://docs.textcontrol.com/tx-sheet-control/reference/javascript-api/browser/workbook/previewremoveduplicates-method/llms-full.txt) previews the retained and removed rows without changing the data or the history. The option `hasHeaders: true` sets the header policy of the range.
- The result is a [RemoveDuplicatesPreview](https://docs.textcontrol.com/tx-sheet-control/reference/typescript-api/browser/removeduplicatespreview/llms-full.txt), the plan of the removal, which the code writes to the console.
- After you have confirmed the keys and options, [removeDuplicates](https://docs.textcontrol.com/tx-sheet-control/reference/javascript-api/browser/workbook/removeduplicates-method/llms-full.txt) removes the duplicates in one undo operation.

Use the relevant options reference, such as [RemoveDuplicatesOptions](https://docs.textcontrol.com/tx-sheet-control/reference/typescript-api/browser/removeduplicatesoptions/llms-full.txt), to select the comparison columns and policies for your data. Transformations are bounded operations and participate in supported transactions and undo. Avoid applying a preview from one document to another without recomputing it.

### Sort Typed Values

Sort and compare typed numbers as numbers. How dates behave depends on serial or ISO values, the date system and explicit number formatting. A formatted currency or date string is not automatically equivalent to a numeric or date value.

### Next Steps

- [Cells and Ranges](https://docs.textcontrol.com/tx-sheet-control/spreadsheets/cells/llms-full.txt): write typed values instead of formatted strings.
- [Tables](https://docs.textcontrol.com/tx-sheet-control/spreadsheets/tables/llms-full.txt): give the data a table with named columns and filter buttons.
- [Dates and Locales](https://docs.textcontrol.com/tx-sheet-control/spreadsheets/dates/llms-full.txt): see how serial values, date systems and formats affect dates.
- [Filters and Drill-Down](https://docs.textcontrol.com/tx-sheet-control/dashboards/filters/llms-full.txt): filter the analytics of a dashboard without changing the worksheet.

---

## Nearby

- **Previous:** [Tables](https://docs.textcontrol.com/tx-sheet-control/spreadsheets/tables/llms-full.txt)
- **Next:** [Data Validation](https://docs.textcontrol.com/tx-sheet-control/spreadsheets/validation/llms-full.txt)
