﻿<head><script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-4357510538660394"
     crossorigin="anonymous"></script></head># sortItems

> This document is the Markdown version of [valibot.dev/api/sortItems/](https://valibot.dev/api/sortItems/). For the complete documentation index, see [llms.txt](https://valibot.dev/llms.txt).

Creates a sort items transformation action.

```ts
const Action = v.sortItems<TInput>(operation);
```

## Generics

- `TInput` `extends ArrayInput`

## Parameters

- `operation` `((itemA: TInput[number], itemB: TInput[number]) => number) | undefined`

### Explanation

With `sortItems` you can sort the items of an array based on a custom `operation`. This is a function that takes two items and returns a number. If the number is less than 0, the first item is sorted before the second item. If the number is greater than 0, the second item is sorted before the first. If the number is 0, the order of the items is not changed.

## Returns

- `Action` `SortItemsAction<TInput>`

## Examples

The following examples show how `sortItems` can be used.

### Sort numbers

Schema that sorts the numbers in an array in ascending order.

```ts
const SortedArraySchema = v.pipe(v.array(v.number()), v.sortItems());
```

## Related

The following APIs can be combined with `sortItems`.

### Schemas

[`any`](/api/any.md), [`array`](/api/array.md), [`custom`](/api/custom.md), [`instance`](/api/instance.md)