# SortState

> Represents the active sort attribute and direction parsed from the sort query value.

[API reference](/docs/api/index.md) › [Debug Core](/docs/extensions/debug-core/reference/index.md) › [View](/docs/extensions/debug-core/reference/view/index.md) › [Grid](/docs/extensions/debug-core/reference/view/grid/index.md) › SortState

| final readonly class | `PHPForge\Debug\View\Grid\SortState` |
| --- | --- |
| Source | [`src/View/Grid/SortState.php`](https://github.com/php-forge/debug-core/blob/main/src/View/Grid/SortState.php#L20) |

Represents the active sort attribute and direction parsed from the `sort` query value.

## Properties

| Property | Type | Description | Defined by |
| --- | --- | --- | --- |
| <span id="property-attribute"></span>`$attribute` readonly constructor | `string` | Attribute the rows are ordered by. | `SortState` |
| <span id="property-direction"></span>`$direction` readonly constructor | `"asc"\|"desc"` | Order applied to the attribute. | `SortState` |

## Methods

| Method | Description | Defined by |
| --- | --- | --- |
| [`apply()`](#method-apply) | Sorts rows with the natural ascending comparator, inverted for `desc`, then breaks ties in a fixed order. | `SortState` |
| [`fromQuery()`](#method-fromQuery) static | Parses the query value, keeping it only when the attribute is sortable; otherwise applies the default. | `SortState` |
| [`header()`](#method-header) | Builds the header cell of a sortable column, linking to the order the next click on it requests. | `SortState` |
| [`panelUrl()`](#method-panelUrl) static | Builds the header-link factory of a panel grid, carrying the query parameters of the visible page. | `SortState` |

## Method details

### apply() <span id="method-apply"></span>

```php
public function apply(list<PHPForge\Debug\View\Grid\TRow> $rows, Closure(PHPForge\Debug\View\Grid\TRow, PHPForge\Debug\View\Grid\TRow): int $compare, Closure(PHPForge\Debug\View\Grid\TRow, PHPForge\Debug\View\Grid\TRow): int|null $tieBreak = null): list<PHPForge\Debug\View\Grid\TRow>
```

Sorts rows with the natural ascending comparator, inverted for `desc`, then breaks ties in a fixed order.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$rows` | `list<PHPForge\Debug\View\Grid\TRow>` | Rows to order. |
| `$compare` | [`Closure`](https://www.php.net/manual/en/class.closure.php)`(PHPForge\Debug\View\Grid\TRow, PHPForge\Debug\View\Grid\TRow): int` | Ascending comparator for the active attribute. |
| `$tieBreak` | [`Closure`](https://www.php.net/manual/en/class.closure.php)`(PHPForge\Debug\View\Grid\TRow, PHPForge\Debug\View\Grid\TRow): int\|null` | Direction-independent comparator applied to rows the main comparator considers equal, or `null` to keep their relative input order. |

**Returns**

`list<PHPForge\Debug\View\Grid\TRow>`: Ordered rows.

### fromQuery() <span id="method-fromQuery"></span>

```php
public static function fromQuery(string|null $sort, list<string> $attributes, string $defaultAttribute, "asc"|"desc" $defaultDirection = 'asc'): self
```

Parses the query value, keeping it only when the attribute is sortable; otherwise applies the default.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$sort` | `string\|null` | Raw `sort` query value, optionally prefixed with `-` to request a descending order. |
| `$attributes` | `list<string>` | Sortable attribute names. |
| `$defaultAttribute` | `string` | Attribute used when the query value names no sortable attribute. |
| `$defaultDirection` | `"asc"\|"desc"` | Direction paired with the default attribute. |

**Returns**

`self`: Sort state for the visible page.

### header() <span id="method-header"></span>

```php
public function header(string $attribute, string $label, Closure(string): string $url, bool $descendingFirst = false): PHPForge\Debug\View\Grid\SortHeader
```

Builds the header cell of a sortable column, linking to the order the next click on it requests.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$attribute` | `string` | Attribute backing the header cell. |
| `$label` | `string` | Visible column label. |
| `$url` | [`Closure`](https://www.php.net/manual/en/class.closure.php)`(string): string` | Builds the panel URL requesting a `sort` query value. |
| `$descendingFirst` | `bool` | Whether the first click on an inactive attribute requests a descending order. |

**Returns**

[`SortHeader`](/docs/extensions/debug-core/reference/view/grid/sortheader/index.md): Header cell announcing the active order.

### panelUrl() <span id="method-panelUrl"></span>

```php
public static function panelUrl(PHPForge\Debug\Panel\PanelRenderContext $context, array<array-key, mixed> $queryParams): Closure(string): string
```

Builds the header-link factory of a panel grid, carrying the query parameters of the visible page.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$context` | [`PHPForge\Debug\Panel\PanelRenderContext`](/docs/extensions/debug-core/reference/panel/panelrendercontext/index.md) | State of the debugger request being rendered. |
| `$queryParams` | `array<array-key, mixed>` | Query parameters the header links are built from. |

**Returns**

[`Closure`](https://www.php.net/manual/en/class.closure.php)`(string): string`: Builds the panel URL requesting a `sort` query value.