useSortedKeys (JSON)
Summary
Section titled “Summary”- Rule available since:
v1.9.0 - Diagnostic Category:
assist/source/useSortedKeys - Sources:
- Same as
json/sort-keys
- Same as
How to enable in your editor
Section titled “How to enable in your editor”{ "editor.codeActionsOnSave": { "source.action.useSortedKeys.biome": "explicit", "source.fixAll.biome": "explicit" }}{ "code_actions_on_format": { "source.action.useSortedKeys.biome": true, "source.fixAll.biome": true }}
Use the source action code
source.action.useSortedKeys.biome How to configure
Section titled “How to configure”{ "assist": { "actions": { "source": { "useSortedKeys": "on" } } }}Description
Section titled “Description”Sort the keys of a JSON object in natural order.
Natural order means
that uppercase letters come before lowercase letters (e.g. A < a <
B < b) and numbers are compared in a human way (e.g. 9 < 10).
Examples
Section titled “Examples”{ "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 │ }
Options
Section titled “Options”This actions accepts following options
sortOrder
Section titled “sortOrder”This options supports natural and lexicographic values. Where as natural is the default.
Following will apply the natural sort order.
{ "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 │
Following will apply the lexicographic sort order.
{ "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 │
groupByNesting
Section titled “groupByNesting”When enabled, groups object keys by their value’s nesting depth before sorting alphabetically. Simple values (primitives, single-line arrays, and single-line objects) are sorted first, followed by nested values (multi-line arrays and multi-line objects).
Default:
false
{ "assist": { "actions": { "source": { "useSortedKeys": { "level": "on", "options": { "groupByNesting": true } } } } }}{ "name": "Sample", "details": { "description": "nested" }, "id": 123}code-block.json:1:1 assist/source/useSortedKeys FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ The members are not sorted by nesting level and key.
> 1 │ {
│ ^
> 2 │ “name”: “Sample”,
> 3 │ “details”: {
> 4 │ “description”: “nested”
> 5 │ },
> 6 │ “id”: 123
> 7 │ }
│ ^
8 │
ℹ Safe fix: Sort the members by key.
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 │
Related links
Section titled “Related links”Copyright (c) 2023-present Biome Developers and Contributors.