sort-objects
Enforce sorted objects.
By adhering to this rule, developers can ensure that object keys are consistently sorted, leading to cleaner and more maintainable code. This rule promotes a standardized key ordering across objects, making it easier to navigate and understand the structure of objects within the codebase.
It’s safe. The rule considers spread elements in objects and does not break component functionality.
Important
If you use the sort-keys
rule, it is highly recommended to disable it to avoid conflicts.
Try it out
Options
This rule accepts an options object with the following properties:
type
default:'alphabetical'
Specifies the sorting method.
'alphabetical'
— Sort items alphabetically (e.g., “a” < “b” < “c”).'natural'
— Sort items in a natural order (e.g., “item2” < “item10”).'line-length'
— Sort items by the length of the code line (shorter lines first).
order
default:'asc'
Determines whether the sorted items should be in ascending or descending order.
'asc'
— Sort items in ascending order (A to Z, 1 to 9).'desc'
— Sort items in descending order (Z to A, 9 to 1).
ignoreCase
default:true
Controls whether sorting should be case-sensitive or not.
true
— Ignore case when sorting alphabetically or naturally (e.g., “A” and “a” are the same).false
— Consider case when sorting (e.g., “A” comes before “a”).
partitionByComment
default:false
Allows you to use comments to separate the keys of objects into logical groups. This can help in organizing and maintaining large enums by creating partitions within the enum based on comments.
true
— All comments will be treated as delimiters, creating partitions.false
— Comments will not be used as delimiters.- string — A glob pattern to specify which comments should act as delimiters.
partitionByNewLine
default:false
When true
, the rule will not sort the object’s keys if there is an empty line between them. This can be useful for keeping logically separated groups of keys in their defined order.
const user = {
// Group 1
firstName: 'John',
lastName: 'Doe',
// Group 2
age: 30,
birthDate: '1990-01-01',
// Group 3
email: 'john.doe@example.com',
phone: '555-555-5555'
};
In this example, the partitionByNewLine
option will cause the rule to treat each group of keys (separated by empty lines) independently, preserving their order within each group.
styledComponents
default:true
Determines whether this rule should be applied to styled-components like libraries.
true
— Apply the rule to styled-components.false
— Disable the rule for styled-components.
ignorePattern
default:[]
Allows you to specify names or patterns for object types that should be ignored by this rule. This can be useful if you have specific objects that you do not want to sort.
You can specify their names or a glob pattern to ignore, for example: 'User*'
to ignore all object types whose names begin with the word “User”.
destructureOnly
default:false
Allows you to sort only objects that are part of a destructuring pattern. When set to true
, the rule will apply sorting exclusively to destructured objects, leaving other object declarations unchanged.
groups
default:[]
Allows you to specify a list of object keys groups for sorting. Groups help organize object keys into categories, making your objects more readable and maintainable. Multiple groups can be combined to achieve the desired sorting order.
There are no predefined groups. You must use this with the customGroups
option.
customGroups
default:{}
You can define your own groups for object keys using custom glob patterns for matching.
{
groups: [
+ 'top',
'unknown',
],
+ customGroups: {
+ top: ['id', 'name']
+ }
}
Usage
Version
This rule was introduced in v0.6.0.