Skip to content

useSortedKeys (JSON)

.vscode/settings.json
{
"editor.codeActionsOnSave": {
"source.action.useSortedKeys.biome": "explicit",
"source.fixAll.biome": "explicit"
}
}
biome.json
{
"assist": {
"actions": {
"source": {
"useSortedKeys": "on"
}
}
}
}

Sort keys in a JSON object.

By default, the action uses natural order: uppercase letters come before lowercase letters (A before a), and numbers are compared by value (item9 before item10).

{
"vase": "fancy",
"nested": {
"omega": "bar",
"alpha": "foo"
}
}
code-block.json ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

ℹ Source action diff:

1 1 │ {
2 │ - ····"vase":·"fancy",
3 │ - ····"nested":·{
2 │ + ····"nested":·{
4 3 │ "omega": "bar",
5 4 │ "alpha": "foo"
6 │ - ····}
5 │ + ····},
6 │ + ····"vase":·"fancy"
7 7 │ }
8 8 │

code-block.json ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

ℹ Source action diff:

2 2 │ "vase": "fancy",
3 3 │ "nested": {
4 │ - ········"omega":·"bar",
5 │ - ········"alpha":·"foo"
4 │ + ········"alpha":·"foo",
5 │ + ········"omega":·"bar"
6 6 │ }
7 7 │ }

Selects natural or lexicographic ordering. Natural ordering compares numbers by value and is the default. Lexicographic ordering compares keys character by character.

The following configuration uses natural order:

biome.json
{
"assist": {
"actions": {
"source": {
"useSortedKeys": {
"level": "on",
"options": {
"sortOrder": "natural"
}
}
}
}
}
}
{
"val13": 1,
"val1": 1,
"val2": 1,
"val21": 1,
"val11": 1
}
code-block.json ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

ℹ Source action diff:

1 1 │ {
2 │ - ····"val13":·1,
3 │ - ····"val1":·1,
4 │ - ····"val2":·1,
5 │ - ····"val21":·1,
6 │ - ····"val11":·1
2 │ + ····"val1":·1,
3 │ + ····"val2":·1,
4 │ + ····"val11":·1,
5 │ + ····"val13":·1,
6 │ + ····"val21":·1
7 7 │ }
8 8 │

The following configuration uses lexicographic order.

biome.json
{
"assist": {
"actions": {
"source": {
"useSortedKeys": {
"level": "on",
"options": {
"sortOrder": "lexicographic"
}
}
}
}
}
}
{
"val13": 1,
"val1": 1,
"val2": 1,
"val21": 1,
"val11": 1
}
code-block.json ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

ℹ Source action diff:

1 1 │ {
2 │ - ····"val13":·1,
3 │ - ····"val1":·1,
4 │ - ····"val2":·1,
5 │ - ····"val21":·1,
6 │ - ····"val11":·1
2 │ + ····"val1":·1,
3 │ + ····"val11":·1,
4 │ + ····"val13":·1,
5 │ + ····"val2":·1,
6 │ + ····"val21":·1
7 7 │ }
8 8 │

Groups keys by the shape of their values before sorting by name. Strings, numbers, booleans, null, and single-line arrays or objects come first. Multi-line arrays and objects come afterward. Defaults to false.

biome.json
{
"assist": {
"actions": {
"source": {
"useSortedKeys": {
"level": "on",
"options": {
"groupByNesting": true
}
}
}
}
}
}
{
"name": "Sample",
"details": {
"description": "nested"
},
"id": 123
}
code-block.json ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

ℹ Source action diff:

1 1 │ {
2 │ - ····"name":·"Sample",
2 │ + ····"id":·123,
3 │ + ····"name":·"Sample",
3 4 │ "details": {
4 5 │ "description": "nested"
5 │ - ····},
6 │ - ····"id":·123
6 │ + ····}
7 7 │ }
8 8 │