Skip to content

useSortedKeys

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

Sorts the keys of a JSON object in natural order

{
"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 }

This actions accepts following options

This options supports natural and lexicographic values. Where as natural is the default.

Following will apply the natural sort order.

{
"options": {
"sortOrder": "natural"
}
}
{
"val13": 1,
"val1": 1,
"val2": 1,
"val21": 1,
"val11": 1,
}
code-block.json:7:1 parse ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Expected a property but instead found ’}‘.

5 │ “val21”: 1,
6 │ “val11”: 1,
> 7 │ }
^
8 │

Expected a property here.

5 │ “val21”: 1,
6 │ “val11”: 1,
> 7 │ }
^
8 │

Following will apply the lexicographic sort order.

{
"options": {
"sortOrder": "lexicographic"
}
}
{
"val13": 1,
"val1": 1,
"val2": 1,
"val21": 1,
"val11": 1,
}
code-block.json:7:1 parse ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Expected a property but instead found ’}‘.

5 │ “val21”: 1,
6 │ “val11”: 1,
> 7 │ }
^
8 │

Expected a property here.

5 │ “val21”: 1,
6 │ “val11”: 1,
> 7 │ }
^
8 │

biome.json
{
"assist": {
"actions": {
"source": {
"useSortedKeys": "on"
}
}
}
}