# JSON array sort objects

`json-array-sort-objects` · version 1.0.0 · JSON & JSONL · free, no key needed

Stably sort an array of objects by explicit string keys using UTF-16 lexicographic order.

**Use when you need to: json array sort objects · sort json objects by keys · stable sort object array.**

## Supported

- json array sort objects
- sort json objects by keys
- stable sort object array
- order json objects lexicographically

## Not supported

- numeric sort
- locale sort
- nested paths
- json pointer sort
- fuzzy matching
- read files
- fetch urls

## Behavior

- items is an array of non-array objects; extra own properties are allowed and preserved.
- keys is a unique non-empty list of own string property names used as the sort tuple, in listed precedence.
- Every listed key must be an own property of every item and its value must be a string; numbers, null, and missing keys are rejected.
- order is asc by default or desc; comparison uses ordinary UTF-16 code-unit < and >, not localeCompare.
- Equal key tuples keep original array order (stable) in both directions.
- Each output item is a new null-prototype object whose own keys match the input item in Object.keys order.

## Input

- `items` (array of object, required): max items 5000
- `keys` (array of string, required): min items 1; max items 16; each min length 1
- `order` (one of "asc", "desc", optional): default `"asc"`

## Output

- `items` (array of object, required): max items 5000

## Limits

- max items: 5000
- max keys: 16
- max depth: 16
- max output bytes: 1048576

## Example

Request input:

```json
{
  "items": [
    {
      "team": "b",
      "name": "Ada"
    },
    {
      "team": "a",
      "name": "Zoe"
    },
    {
      "team": "a",
      "name": "Ada"
    }
  ],
  "keys": [
    "team",
    "name"
  ]
}
```

Response:

```json
{
  "result": {
    "items": [
      {
        "team": "a",
        "name": "Ada"
      },
      {
        "team": "a",
        "name": "Zoe"
      },
      {
        "team": "b",
        "name": "Ada"
      }
    ]
  }
}
```

## How to call it

### MCP

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

```json
{
  "id": "json-array-sort-objects",
  "version": "1.0.0",
  "input": {
    "items": [
      {
        "team": "b",
        "name": "Ada"
      },
      {
        "team": "a",
        "name": "Zoe"
      },
      {
        "team": "a",
        "name": "Ada"
      }
    ],
    "keys": [
      "team",
      "name"
    ]
  }
}
```

### HTTP (no key)

```sh
curl -X POST https://computefirst.net/v1/tools/json-array-sort-objects/versions/1.0.0/execute \
  -H "Content-Type: application/json" \
  -d '{"items":[{"team":"b","name":"Ada"},{"team":"a","name":"Zoe"},{"team":"a","name":"Ada"}],"keys":["team","name"]}'
```

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

### CLI

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

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

## Related tools

- [JSON array group by](/tools/json-array-group-by): Group an array of JSON objects by exact keys.
- [JSON array anti join](/tools/json-array-anti-join): Anti join two JSON arrays of objects on exact keys.
- [JSON array distinct by](/tools/json-array-distinct-by): Deduplicate a JSON array of objects based on specific keys.
- [JSON array filter equals](/tools/json-array-filter-equals): Filter JSON array objects where a key equals a specific value.
- [JSON array group count](/tools/json-array-group-count): Group an array of JSON objects by exact keys and return the count.
- [JSON array inner join](/tools/json-array-inner-join): Inner join two JSON arrays of objects on exact keys.
