jsonc/sort-array-values
require array values to be sorted
- 🔧 The
--fixoption on the command line can automatically fix some of the problems reported by this rule.
📖 Rule Details
This rule checks values of array and verifies that values are sorted alphabetically or specified order.
🔧 Options
json5
{
"jsonc/sort-array-values": ["error",
{
"pathPattern": "^files$", // Hits the files property
"order": { "type": "asc" }
},
{
"pathPattern": "^keywords$", // Hits the keywords property
"order": [
"eslint",
"eslintplugin",
"eslint-plugin",
{
// Fallback order
"order": { "type": "asc" }
}
]
},
{
"pathPattern": "^categories\\.\\w+$", // Hits arrays of objects
// Sort each array of objects by its "name" property
"order": { "type": "asc", "key": "name" }
},
{
"pathPattern": "^items$",
"order": [
{
// Sort object elements by their "name" property
"order": { "type": "asc", "key": "name" }
},
{
// Sort any remaining values alphabetically
"order": { "type": "asc" }
}
]
}
]
}The option receives multiple objects with the following properties:
pathPattern(Required) ... Defines the regular expression pattern of paths to which you want to enforce the order. If you want to apply to the top level, define"^$".order(Required) ... Defines how to enforce the order. You can use an object or an array.- Array ... Defines an array of values to enforce the order.
- String ... Defines the value.
- Object ... The object has the following properties:
valuePattern... Defines a pattern to match the value, or the value atorder.key, when defined. Default behavior matches the logic to all values.order... The object has the following properties:type:"asc"... Enforce values to be in ascending order. This is default."desc"... Enforce values to be in descending order.
caseSensitive... Iftrue, enforce values to be in case-sensitive order. Default istrue.natural... Iftrue, enforce values to be in natural order. Default isfalse.key... Sorts an array of objects by the value of this property. Elements that lack the property are left in place and not reordered. Default is to compare the element value itself.
- Object ... The object has the following properties:
type:"asc"... Enforce values to be in ascending order. This is default."desc"... Enforce values to be in descending order.
caseSensitive... Iftrue, enforce values to be in case-sensitive order. Default istrue.natural... Iftrue, enforce values to be in natural order. Default isfalse.key... Sorts an array of objects by the value of this property. Elements that lack the property are left in place and not reordered. Default is to compare the element value itself.
- Array ... Defines an array of values to enforce the order.
minValues... Specifies the minimum number of values that an array should have in order for the array's unsorted values to produce an error. Default is2, which means by default all arrays with unsorted values will result in lint errors.
👫 Related rules
🚀 Version
This rule was introduced in eslint-plugin-jsonc v2.2.0