# Workbook.SortRangeAsync Method

> Workbook.SortRangeAsync method in the TX Sheet Control .NET API: Sorts an A1 range on the active worksheet using validated sort keys.

- **Product:** TX Sheet Control
- **Section:** Documentation
- **Kind:** Method
- **Namespace:** `TXTextControl.TXSheetControl.Server`
- **Package:** `TXTextControl.TXSheetControl.Server` (assembly `TXTextControl.TXSheetControl.Api.dll`, from the private NuGet feed or the supplied NuGet folder)
- **Package:** `TXTextControl.TXSheetControl.Blazor` (assembly `TXTextControl.TXSheetControl.Api.dll`, from the private NuGet feed or the supplied NuGet folder)
- **Path:** TX Sheet Control > API Reference > .NET API > TXTextControl.TXSheetControl.Server > Workbook > Workbook.SortRangeAsync Method
- **775 words**
- **Web URL:** https://docs.textcontrol.com/tx-sheet-control/reference/dotnet-api/txtextcontrol-txsheetcontrol-server/workbook/sortrangeasync-method/
- **LLMs URL:** https://docs.textcontrol.com/tx-sheet-control/reference/dotnet-api/txtextcontrol-txsheetcontrol-server/workbook/sortrangeasync-method/llms.txt
- **LLMs-Full URL:** https://docs.textcontrol.com/tx-sheet-control/reference/dotnet-api/txtextcontrol-txsheetcontrol-server/workbook/sortrangeasync-method/llms-full.txt

---

Sorts an A1 range on the active worksheet using validated sort keys; preserves row relationships and records one undo operation.

### Syntax

```csharp
public Task SortRangeAsync(string range, object options, CancellationToken cancellationToken = default)
```

### Parameters

| Parameter | Type | Optional | Description |
|---|---|---|---|
| `range` | `string` | No | A1 range on the owning worksheet. |
| `options` | `object` | No | Supported settings for this operation; omitted settings retain documented defaults. |
| `cancellationToken` | `CancellationToken` | Yes | Cancels the pending host operation. |

### Return Value

`Task`.

### Examples

Create the sample document data first, then use Workbook.SortRangeAsync. The returned value can be used by the calling report or application.

```csharp
using TXTextControl.TXSheetControl.Server;

async Task UseWorkbookSortRangeAsync(ServerSheetControl control)
{
    var book = control.Workbook;
    var sheet = await book.Sheets.AtAsync(0);
    await sheet.Cell("B8").SetValueAsync(120);
    await sheet.Cell("B9").SetValueAsync(80);
    await sheet.Cell("B10").SetFormulaAsync("=SUM(B8:B9)");

    var workbook = book;

    await workbook.SortRangeAsync("A1:B5", new { keys = new object?[] { new { column = "B", order = "descending" } }, hasHeaders = true });
}
```

### Remarks

Invalid ranges, definitions and protected edits fail through ServerSheetControlException.

Use the member with an instance or settings object from the documented package. Await task-returning operations. Document-bound handles must be resolved again after loading or deleting their owner.

### See Also

- [Workbook Class](https://docs.textcontrol.com/tx-sheet-control/reference/dotnet-api/txtextcontrol-txsheetcontrol-server/workbook/llms-full.txt)
- [Workbooks and Worksheets](https://docs.textcontrol.com/tx-sheet-control/spreadsheets/workbooks/llms-full.txt)

---

## Nearby

- **Previous:** [Workbook.Sheets Property](https://docs.textcontrol.com/tx-sheet-control/reference/dotnet-api/txtextcontrol-txsheetcontrol-server/workbook/sheets-property/llms-full.txt)
- **Next:** [Workbook.TextToColumnsAsync Method](https://docs.textcontrol.com/tx-sheet-control/reference/dotnet-api/txtextcontrol-txsheetcontrol-server/workbook/texttocolumnsasync-method/llms-full.txt)
