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”).
specialCharacters
default:keep
Controls whether special characters should be trimmed, removed or kept before sorting.
'keep'
— Keep special characters when sorting (e.g., “_a” comes before “a”).'trim'
— Trim special characters when sorting alphabetically or naturally (e.g., “_a” and “a” are the same).'remove'
— Remove special characters when sorting (e.g., “/a/b” and “ab” are the same).
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.string[]
— An array of glob patterns 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'
};
Each group of keys (separated by empty lines) is treated independently, and the order within each group is preserved.
styledComponents
default:true
Determines whether this rule should be applied to styled-components like libraries or style
JSX attribute.
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
type: Array<string | string[]>
[]
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.
Predefined groups:
'unknown'
— Properties that don’t fit into any group specified in thegroups
option.
If the unknown
group is not specified in the groups
option, it will automatically be added to the end of the list.
Each object member will be assigned a single group specified in the groups
option (or the unknown
group if no match is found). The order of items in the groups
option determines how groups are ordered.
Within a given group, members will be sorted according to the type
, order
, ignoreCase
, etc. options.
Individual groups can be combined together by placing them in an array. The order of groups in that array does not matter. All members of the groups in the array will be sorted together as if they were part of a single group.
customGroups
type: { [groupName: string]: string | string[] }
{}
You can define your own groups and use custom glob patterns or regex to match specific object keys.
Use the matcher
option to specify the pattern matching method.
Each key of customGroups
represents a group name which you can then use in the groups
option. The value for each key can either be of type:
string
— An object attribute’s name matching the value will be marked as part of the group referenced by the key.string[]
— An object attribute’s name matching any of the values of the array will be marked as part of the group referenced by the key. The order of values in the array does not matter.
Custom group matching takes precedence over predefined group matching.
Example
Put all properties starting with id
and name
at the top, put metadata at the bottom. Anything else is put in the middle.
const user = {
id: 'id', // top
name: 'John', // top
age: 40, // unknown
isAdmin: false, // unknown
localization: { // unknown
// Stuff about localization
},
lastUpdated_metadata: null, // bottom
version_metadata: '1' // bottom
}
groups
and customGroups
configuration:
{
groups: [
+ 'top',
'unknown',
'bottom'
],
+ customGroups: {
+ top: ['id', 'name']
+ bottom: '*_metadata'
+ }
}
matcher
default:'minimatch'
Determines the matcher used for patterns in the partitionByComment
, ignorePattern
and customGroups
options.
'minimatch'
— Use the minimatch library for pattern matching.'regex'
— Use regular expressions for pattern matching.
Usage
Version
This rule was introduced in v0.6.0.