# CSV sort

`csv-sort` · version 1.0.0 · CSV & tables · free, no key needed

Stably sort CSV rows by exact named string columns.

**Use when you need to: csv sort · sort csv rows · sort csv ascending.**

## Supported

- csv sort
- sort csv rows
- sort csv ascending
- sort csv descending
- sort csv by columns

## Not supported

- numeric sort
- locale sort
- natural sort
- fuzzy matching
- mixed sort directions
- read files
- fetch urls

## Behavior

- key_columns names unique existing headers exactly; listed order determines multi-key precedence.
- Values are compared lexicographically by JavaScript UTF-16 code units without locale or numeric coercion.
- order is ascending by default and may be descending; equal keys retain input order in either direction.
- Header-only input is supported; an empty document is rejected because it has no header.
- The first header name may not begin with U+FEFF because it is ambiguous with a CSV byte-order mark.

## Input

- `csv` (string, required): max length 256000
- `key_columns` (array of string, required): min items 1; max items 256; each min length 1
- `order` (one of "ascending", "descending", optional): default `"ascending"`

## Output

- string

## Limits

- max input bytes: 256000
- max data rows: 5000
- max columns: 256
- max header name bytes: 256
- max key columns: 256
- max output bytes: 256000

## Example

Request input:

```json
{
  "csv": "team,name\nb,Ada\na,Zoe\na,Ada\n",
  "key_columns": [
    "team",
    "name"
  ]
}
```

Response:

```json
{
  "result": "team,name\na,Ada\na,Zoe\nb,Ada\n"
}
```

## How to call it

### MCP

Connect `https://computefirst.net/mcp` ([setup](/docs#connect)), then call `execute` with:

```json
{
  "id": "csv-sort",
  "version": "1.0.0",
  "input": {
    "csv": "team,name\nb,Ada\na,Zoe\na,Ada\n",
    "key_columns": [
      "team",
      "name"
    ]
  }
}
```

### HTTP (no key)

```sh
curl -X POST https://computefirst.net/v1/tools/csv-sort/versions/1.0.0/execute \
  -H "Content-Type: application/json" \
  -d '{"csv":"team,name\nb,Ada\na,Zoe\na,Ada\n","key_columns":["team","name"]}'
```

The machine-readable contract is at [/v1/tools/csv-sort/versions/1.0.0](/v1/tools/csv-sort/versions/1.0.0).

### CLI

```sh
node cli.mjs run csv-sort 1.0.0 --input input.json --base-url https://computefirst.net
```

Get the client at [/clients/cli/](/clients/cli/).

## Related tools

- [CSV dedupe](/tools/csv-dedupe): Remove duplicate CSV data rows while preserving the first occurrence.
- [CSV group count](/tools/csv-group-count): Count CSV rows by exact named-column tuples in first-seen order.
- [CSV row diff](/tools/csv-row-diff): Compare two CSV documents with identical headers by unique key tuples and report added, removed, and changed rows.
- [CSV split column](/tools/csv-split-column): Split one CSV column on an exact separator into uniquely named columns.
- [CSV transpose](/tools/csv-transpose): Transpose a CSV matrix including the header row, then re-validate headers.
- [CSV add row number](/tools/csv-add-row-number): Prepend a 1-based row_number column of canonical integer strings.
