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 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).
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 β
Options
Section titled βOptionsβsortOrder
Section titled βsortOrderβ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:
{ "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.
{ "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β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.
{ "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 β
Related links
Section titled βRelated linksβCopyright (c) 2023-present Biome Developers and Contributors.