sort-keys
Enforce alphabetical sorting of object keys.
Targeted pattern scopeโ
This rule checks object literals (ObjectExpression) and evaluates non-computed
initializer properties (Property with kind: "init").
Supported sortable keys are:
- identifier keys (
foo) - string literal keys (
"foo")
Computed keys, spread entries, and accessor properties are not part of sorting.
What this rule reportsโ
This rule reports object properties that are out of alphabetical order.
An autofix is provided and rewrites the property sequence in sorted order.
Why this rule existsโ
Sorted keys make large object literals easier to navigate and reduce churn when multiple contributors add new properties.
โ Incorrectโ
const config = {
zIndex: 10,
align: "start",
};
โ Correctโ
const config = {
align: "start",
zIndex: 10,
};
Behavior and migration notesโ
This rule is autofixable for supported property shapes.
If an object contains unsupported sortable keys, the rule may skip reporting for that object to avoid unsafe transformations.
Optionsโ
This rule has no options.
Additional examplesโ
const tokens = {
"a-color": "#000",
zColor: "#fff",
};
// โ
mixed identifier/string keys are sorted by key text
const partial = {
b: 2,
[dynamicKey]: 1,
a: 1,
};
// โ reported: non-computed initializer keys (`b`, `a`) are still checked
ESLint flat config exampleโ
import etcMisc from "eslint-plugin-etc-misc";
export default [
{
plugins: { "etc-misc": etcMisc },
rules: {
"etc-misc/sort-keys": "error",
},
},
];
When not to use itโ
Disable this rule when property order communicates intent (for example, display sections, execution sequence, or business-priority grouping).
Package documentationโ
Rule catalog ID: R070
Further readingโ
Adoption resourcesโ
- Start at warning level in CI, then move to error after cleanup.
- Use focused codemods/autofix batches per package or directory.