useSortedKeys (JavaScript)
Summary
Section titled “Summary”- Rule available since:
v2.0.0 - Diagnostic Category:
assist/source/useSortedKeys - Sources:
- Inspired from
sort-keys - Inspired from
perfectionist/sort-objects
- Inspired from
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 }}source.action.useSortedKeys.biome How to configure
Section titled “How to configure”{ "assist": { "actions": { "source": { "useSortedKeys": "on" } } }}Description
Section titled “Description”Sort the properties of a JavaScript 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).
Spread properties such as ...other and computed property names such as [key] cannot be
safely reordered. Each one forms a boundary; the action sorts named properties on each side
independently. This preserves the order in which spread properties can replace earlier
values.
Object property order is observable. Sorting can change the results of
Object.getOwnPropertyNames, Object.keys, JSON.stringify, and for...in loops. If code
relies on the original order, disable the action with a suppression comment:
// biome-ignore assist/source/useSortedKeys
Examples
Section titled “Examples”const obj = { x: 1, a: 2,};code-block.js ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Source action diff:
1 1 │ const obj = {
2 │ - ··x:·1,
3 │ - ··a:·2,
2 │ + ··a:·2,
3 │ + ··x:·1,
4 4 │ };
5 5 │
const obj = { x: 1, ...f, y: 4, a: 2, [calculated()]: true, b: 3, a: 1,};code-block.js ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Source action diff:
2 2 │ x: 1,
3 3 │ ...f,
4 │ - ··y:·4,
5 │ - ··a:·2,
4 │ + ··a:·2,
5 │ + ··y:·4,
6 6 │ [calculated()]: true,
7 │ - ··b:·3,
8 │ - ··a:·1,
7 │ + ··a:·1,
8 │ + ··b:·3,
9 9 │ };
10 10 │
const obj = { get aab() { return this._aab; }, set aac(v) { this._aac = v; }, w: 1, x: 1, ...g, get aaa() { return ""; }, u: 1, v: 1, [getProp()]: 2, o: 1, p: 1, q: 1,}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 property names character by character.
The following configuration uses natural order:
{ "assist": { "actions": { "source": { "useSortedKeys": { "level": "on", "options": { "sortOrder": "natural" } } } } }}const obj = { val13: 1, val1: 1, val2: 1, val21: 1, val11: 1,};code-block.js ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Source action diff:
1 1 │ const obj = {
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" } } } } }}const obj = { val13: 1, val1: 1, val2: 1, val21: 1, val11: 1,};code-block.js ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Source action diff:
1 1 │ const obj = {
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 properties by the shape of their values before sorting by name. Simple values, such
as strings, numbers, booleans, null, and single-line arrays or objects, come first.
Multi-line arrays and objects, functions, classes, and object methods come afterward.
Defaults to false.
{ "assist": { "actions": { "source": { "useSortedKeys": { "level": "on", "options": { "groupByNesting": true } } } } }}const obj = { name: "Sample", details: { description: "nested" }, id: 123};code-block.js ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Source action diff:
1 1 │ const obj = {
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.