11 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.
15 This rule checks all property definitions of object expressions and verifies that all variables are sorted alphabetically.
17 Examples of **incorrect** code for this rule:
22 /*eslint sort-keys: "error"*/
25 let obj = {a: 1, c: 3, b: 2};
26 let obj = {a: 1, "c": 3, b: 2};
28 // Case-sensitive by default.
29 let obj = {a: 1, b: 2, C: 3};
31 // Non-natural order by default.
32 let obj = {1: a, 2: c, 10: b};
34 // This rule checks computed properties which have a simple name as well.
35 // Simple names are names which are expressed by an Identifier node or a Literal node.
37 let obj = {a: 1, ["c"]: 3, b: 2};
38 let obj = {a: 1, [S]: 3, b: 2};
43 Examples of **correct** code for this rule:
48 /*eslint sort-keys: "error"*/
51 let obj = {a: 1, b: 2, c: 3};
52 let obj = {a: 1, "b": 2, c: 3};
54 // Case-sensitive by default.
55 let obj = {C: 3, a: 1, b: 2};
57 // Non-natural order by default.
58 let obj = {1: a, 10: b, 2: c};
60 // This rule checks computed properties which have a simple name as well.
61 let obj = {a: 1, ["b"]: 2, c: 3};
62 let obj = {a: 1, [b]: 2, c: 3};
64 // This rule ignores computed properties which have a non-simple name.
65 let obj = {a: 1, [c + d]: 3, b: 2};
66 let obj = {a: 1, ["c" + "d"]: 3, b: 2};
67 let obj = {a: 1, [`${c}`]: 3, b: 2};
68 let obj = {a: 1, [tag`c`]: 3, b: 2};
70 // This rule does not report unsorted properties that are separated by a spread property.
71 let obj = {b: 1, ...c, a: 2};
80 "sort-keys": ["error", "asc", {"caseSensitive": true, "natural": false, "minKeys": 2}]
84 The 1st option is `"asc"` or `"desc"`.
86 * `"asc"` (default) - enforce properties to be in ascending order.
87 * `"desc"` - enforce properties to be in descending order.
89 The 2nd option is an object which has 3 properties.
91 * `caseSensitive` - if `true`, enforce properties to be in case-sensitive order. Default is `true`.
92 * `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.
93 * `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.
94 * `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`.
98 With `natural` as true, the ordering would be
105 With `natural` as false, the ordering would be
114 Examples of **incorrect** code for the `"desc"` option:
119 /*eslint sort-keys: ["error", "desc"]*/
122 let obj = {b: 2, c: 3, a: 1};
123 let obj = {"b": 2, c: 3, a: 1};
125 // Case-sensitive by default.
126 let obj = {C: 1, b: 3, a: 2};
128 // Non-natural order by default.
129 let obj = {10: b, 2: c, 1: a};
134 Examples of **correct** code for the `"desc"` option:
139 /*eslint sort-keys: ["error", "desc"]*/
142 let obj = {c: 3, b: 2, a: 1};
143 let obj = {c: 3, "b": 2, a: 1};
145 // Case-sensitive by default.
146 let obj = {b: 3, a: 2, C: 1};
148 // Non-natural order by default.
149 let obj = {2: c, 10: b, 1: a};
156 Examples of **incorrect** code for the `{caseSensitive: false}` option:
161 /*eslint sort-keys: ["error", "asc", {caseSensitive: false}]*/
164 let obj = {a: 1, c: 3, C: 4, b: 2};
165 let obj = {a: 1, C: 3, c: 4, b: 2};
170 Examples of **correct** code for the `{caseSensitive: false}` option:
175 /*eslint sort-keys: ["error", "asc", {caseSensitive: false}]*/
178 let obj = {a: 1, b: 2, c: 3, C: 4};
179 let obj = {a: 1, b: 2, C: 3, c: 4};
186 Examples of **incorrect** code for the `{natural: true}` option:
191 /*eslint sort-keys: ["error", "asc", {natural: true}]*/
194 let obj = {1: a, 10: c, 2: b};
199 Examples of **correct** code for the `{natural: true}` option:
204 /*eslint sort-keys: ["error", "asc", {natural: true}]*/
207 let obj = {1: a, 2: b, 10: c};
214 Examples of **incorrect** code for the `{minKeys: 4}` option:
219 /*eslint sort-keys: ["error", "asc", {minKeys: 4}]*/
225 a: 1, // not sorted correctly (should be 1st key)
233 1: 'b', // not sorted correctly (should be 1st key)
242 Examples of **correct** code for the `{minKeys: 4}` option:
247 /*eslint sort-keys: ["error", "asc", {minKeys: 4}]*/
266 ### allowLineSeparatedGroups
268 Examples of **incorrect** code for the `{allowLineSeparatedGroups: true}` option:
273 /*eslint sort-keys: ["error", "asc", {allowLineSeparatedGroups: true}]*/
307 // comment before comma
314 Examples of **correct** code for the `{allowLineSeparatedGroups: true}` option:
319 /*eslint sort-keys: ["error", "asc", {allowLineSeparatedGroups: true}]*/
371 // comment before comma
388 ## When Not To Use It
390 If you don't want to notify about properties' order, then it's safe to disable this rule.
394 * **JSCS:** [validateOrderInObjectKeys](https://jscs-dev.github.io/rule/validateOrderInObjectKeys)