1 # require object keys to be sorted (sort-keys)
3 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.
7 This rule checks all property definitions of object expressions and verifies that all variables are sorted alphabetically.
9 Examples of **incorrect** code for this rule:
12 /*eslint sort-keys: "error"*/
15 let obj = {a: 1, c: 3, b: 2};
16 let obj = {a: 1, "c": 3, b: 2};
18 // Case-sensitive by default.
19 let obj = {a: 1, b: 2, C: 3};
21 // Non-natural order by default.
22 let obj = {1: a, 2: c, 10: b};
24 // This rule checks computed properties which have a simple name as well.
25 // Simple names are names which are expressed by an Identifier node or a Literal node.
27 let obj = {a: 1, ["c"]: 3, b: 2};
28 let obj = {a: 1, [S]: 3, b: 2};
31 Examples of **correct** code for this rule:
34 /*eslint sort-keys: "error"*/
37 let obj = {a: 1, b: 2, c: 3};
38 let obj = {a: 1, "b": 2, c: 3};
40 // Case-sensitive by default.
41 let obj = {C: 3, a: 1, b: 2};
43 // Non-natural order by default.
44 let obj = {1: a, 10: b, 2: c};
46 // This rule checks computed properties which have a simple name as well.
47 let obj = {a: 1, ["b"]: 2, c: 3};
48 let obj = {a: 1, [b]: 2, c: 3};
50 // This rule ignores computed properties which have a non-simple name.
51 let obj = {a: 1, [c + d]: 3, b: 2};
52 let obj = {a: 1, ["c" + "d"]: 3, b: 2};
53 let obj = {a: 1, [`${c}`]: 3, b: 2};
54 let obj = {a: 1, [tag`c`]: 3, b: 2};
56 // This rule does not report unsorted properties that are separated by a spread property.
57 let obj = {b: 1, ...c, a: 2};
64 "sort-keys": ["error", "asc", {"caseSensitive": true, "natural": false, "minKeys": 2}]
68 The 1st option is `"asc"` or `"desc"`.
70 * `"asc"` (default) - enforce properties to be in ascending order.
71 * `"desc"` - enforce properties to be in descending order.
73 The 2nd option is an object which has 3 properties.
75 * `caseSensitive` - if `true`, enforce properties to be in case-sensitive order. Default is `true`.
76 * `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.
77 * `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.
81 With `natural` as true, the ordering would be
88 With `natural` as false, the ordering would be
97 Examples of **incorrect** code for the `"desc"` option:
100 /*eslint sort-keys: ["error", "desc"]*/
103 let obj = {b: 2, c: 3, a: 1};
104 let obj = {"b": 2, c: 3, a: 1};
106 // Case-sensitive by default.
107 let obj = {C: 1, b: 3, a: 2};
109 // Non-natural order by default.
110 let obj = {10: b, 2: c, 1: a};
113 Examples of **correct** code for the `"desc"` option:
116 /*eslint sort-keys: ["error", "desc"]*/
119 let obj = {c: 3, b: 2, a: 1};
120 let obj = {c: 3, "b": 2, a: 1};
122 // Case-sensitive by default.
123 let obj = {b: 3, a: 2, C: 1};
125 // Non-natural order by default.
126 let obj = {2: c, 10: b, 1: a};
131 Examples of **incorrect** code for the `{caseSensitive: false}` option:
134 /*eslint sort-keys: ["error", "asc", {caseSensitive: false}]*/
137 let obj = {a: 1, c: 3, C: 4, b: 2};
138 let obj = {a: 1, C: 3, c: 4, b: 2};
141 Examples of **correct** code for the `{caseSensitive: false}` option:
144 /*eslint sort-keys: ["error", "asc", {caseSensitive: false}]*/
147 let obj = {a: 1, b: 2, c: 3, C: 4};
148 let obj = {a: 1, b: 2, C: 3, c: 4};
153 Examples of **incorrect** code for the `{natural: true}` option:
156 /*eslint sort-keys: ["error", "asc", {natural: true}]*/
159 let obj = {1: a, 10: c, 2: b};
162 Examples of **correct** code for the `{natural: true}` option:
165 /*eslint sort-keys: ["error", "asc", {natural: true}]*/
168 let obj = {1: a, 2: b, 10: c};
173 Examples of **incorrect** code for the `{minKeys: 4}` option:
176 /*eslint sort-keys: ["error", "asc", {minKeys: 4}]*/
182 a: 1, // not sorted correctly (should be 1st key)
190 1: 'b', // not sorted correctly (should be 1st key)
197 Examples of **correct** code for the `{minKeys: 4}` option:
200 /*eslint sort-keys: ["error", "asc", {minKeys: 4}]*//
217 ## When Not To Use It
219 If you don't want to notify about properties' order, then it's safe to disable this rule.
223 * [sort-imports](sort-imports.md)
224 * [sort-vars](sort-vars.md)
228 * **JSCS:** [validateOrderInObjectKeys](https://jscs-dev.github.io/rule/validateOrderInObjectKeys)