10 When declaring multiple properties, some developers prefer to sort property names alphabetically to more easily find and/or diff necessary properties at a later time. Others feel that it adds complexity and becomes burden to maintain.
14 This rule checks all property definitions of object expressions and verifies that all variables are sorted alphabetically.
16 Examples of **incorrect** code for this rule:
21 /*eslint sort-keys: "error"*/
24 let obj = {a: 1, c: 3, b: 2};
25 let obj = {a: 1, "c": 3, b: 2};
27 // Case-sensitive by default.
28 let obj = {a: 1, b: 2, C: 3};
30 // Non-natural order by default.
31 let obj = {1: a, 2: c, 10: b};
33 // This rule checks computed properties which have a simple name as well.
34 // Simple names are names which are expressed by an Identifier node or a Literal node.
36 let obj = {a: 1, ["c"]: 3, b: 2};
37 let obj = {a: 1, [S]: 3, b: 2};
42 Examples of **correct** code for this rule:
47 /*eslint sort-keys: "error"*/
50 let obj = {a: 1, b: 2, c: 3};
51 let obj = {a: 1, "b": 2, c: 3};
53 // Case-sensitive by default.
54 let obj = {C: 3, a: 1, b: 2};
56 // Non-natural order by default.
57 let obj = {1: a, 10: b, 2: c};
59 // This rule checks computed properties which have a simple name as well.
60 let obj = {a: 1, ["b"]: 2, c: 3};
61 let obj = {a: 1, [b]: 2, c: 3};
63 // This rule ignores computed properties which have a non-simple name.
64 let obj = {a: 1, [c + d]: 3, b: 2};
65 let obj = {a: 1, ["c" + "d"]: 3, b: 2};
66 let obj = {a: 1, [`${c}`]: 3, b: 2};
67 let obj = {a: 1, [tag`c`]: 3, b: 2};
69 // This rule does not report unsorted properties that are separated by a spread property.
70 let obj = {b: 1, ...c, a: 2};
79 "sort-keys": ["error", "asc", {"caseSensitive": true, "natural": false, "minKeys": 2}]
83 The 1st option is `"asc"` or `"desc"`.
85 * `"asc"` (default) - enforce properties to be in ascending order.
86 * `"desc"` - enforce properties to be in descending order.
88 The 2nd option is an object which has 3 properties.
90 * `caseSensitive` - if `true`, enforce properties to be in case-sensitive order. Default is `true`.
91 * `minKeys` - Specifies the minimum number of keys that an object should have in order for the object's unsorted keys to produce an error. Default is `2`, which means by default all objects with unsorted keys will result in lint errors.
92 * `natural` - if `true`, enforce properties to be in natural order. Default is `false`. Natural Order compares strings containing combination of letters and numbers in the way a human being would sort. It basically sorts numerically, instead of sorting alphabetically. So the number 10 comes after the number 3 in Natural Sorting.
93 * `allowLineSeparatedGroups` - if `true`, the rule allows to group object keys through line breaks. In other words, a blank line after a property will reset the sorting of keys. Default is `false`.
97 With `natural` as true, the ordering would be
104 With `natural` as false, the ordering would be
113 Examples of **incorrect** code for the `"desc"` option:
118 /*eslint sort-keys: ["error", "desc"]*/
121 let obj = {b: 2, c: 3, a: 1};
122 let obj = {"b": 2, c: 3, a: 1};
124 // Case-sensitive by default.
125 let obj = {C: 1, b: 3, a: 2};
127 // Non-natural order by default.
128 let obj = {10: b, 2: c, 1: a};
133 Examples of **correct** code for the `"desc"` option:
138 /*eslint sort-keys: ["error", "desc"]*/
141 let obj = {c: 3, b: 2, a: 1};
142 let obj = {c: 3, "b": 2, a: 1};
144 // Case-sensitive by default.
145 let obj = {b: 3, a: 2, C: 1};
147 // Non-natural order by default.
148 let obj = {2: c, 10: b, 1: a};
155 Examples of **incorrect** code for the `{caseSensitive: false}` option:
160 /*eslint sort-keys: ["error", "asc", {caseSensitive: false}]*/
163 let obj = {a: 1, c: 3, C: 4, b: 2};
164 let obj = {a: 1, C: 3, c: 4, b: 2};
169 Examples of **correct** code for the `{caseSensitive: false}` option:
174 /*eslint sort-keys: ["error", "asc", {caseSensitive: false}]*/
177 let obj = {a: 1, b: 2, c: 3, C: 4};
178 let obj = {a: 1, b: 2, C: 3, c: 4};
185 Examples of **incorrect** code for the `{natural: true}` option:
190 /*eslint sort-keys: ["error", "asc", {natural: true}]*/
193 let obj = {1: a, 10: c, 2: b};
198 Examples of **correct** code for the `{natural: true}` option:
203 /*eslint sort-keys: ["error", "asc", {natural: true}]*/
206 let obj = {1: a, 2: b, 10: c};
213 Examples of **incorrect** code for the `{minKeys: 4}` option:
218 /*eslint sort-keys: ["error", "asc", {minKeys: 4}]*/
224 a: 1, // not sorted correctly (should be 1st key)
232 1: 'b', // not sorted correctly (should be 1st key)
241 Examples of **correct** code for the `{minKeys: 4}` option:
246 /*eslint sort-keys: ["error", "asc", {minKeys: 4}]*/
265 ### allowLineSeparatedGroups
267 Examples of **incorrect** code for the `{allowLineSeparatedGroups: true}` option:
272 /*eslint sort-keys: ["error", "asc", {allowLineSeparatedGroups: true}]*/
306 // comment before comma
313 Examples of **correct** code for the `{allowLineSeparatedGroups: true}` option:
318 /*eslint sort-keys: ["error", "asc", {allowLineSeparatedGroups: true}]*/
370 // comment before comma
387 ## When Not To Use It
389 If you don't want to notify about properties' order, then it's safe to disable this rule.
393 * **JSCS:** [validateOrderInObjectKeys](https://jscs-dev.github.io/rule/validateOrderInObjectKeys)