Skip to content

sorted-keys

all languagesstylefix: unsafedefault: offyaml/yamllint

Reports the keys of a mapping that are not in order (alphabetical by default). By default the fewest keys that would have to move are reported (report: moved), misplaced reports every key that sorts before one above it. The first report carries the fix. Comments on lines of their own stay where they are and a comment after an entry moves with it. Options: order (keys that come first), direction, compare (ordinal, case_insensitive, natural), ignored_keys, min_keys (default 2), blank_line_groups, path_patterns and has_properties (only mappings that have all of these keys). The fix is unsafe: moving a YAML anchor below its alias breaks the document.

Keys in a fixed order are quick to look up, and two branches that add different keys to a sorted file rarely conflict. The order of a mapping is usually not data, but where a tool reads it (a build order, a dependency list that is applied top down) the rule should be limited with path_patterns or turned off.

To fix, sort the keys; the fix does it.

With this configuration:

sorted-keys:
level: warn
options:
blank_line_groups: true
Reported
{
"b": 1,
"a": 2,
"d": 3,
"c": 4
}
After the fix
{
"a": 2,
"b": 1,
"c": 4,
"d": 3
}
lint:
rules:
sorted-keys:
level: warn # off (default), warn, error
options:
order: []
direction: "asc"
compare: "ordinal"
ignored_keys: []
min_keys: 2
blank_line_groups: false
path_patterns: []
has_properties: []
report: "moved"
Option Type Default Description
order list [] Keys that come first, in this order; the rest follow in the configured order.
direction string "asc" asc or desc.
Values: asc, desc
compare string "ordinal" ordinal (code points), case_insensitive, or natural (digit runs compare as numbers: a2 before a10).
Values: ordinal, case_insensitive, natural
ignored_keys list [] Regular expressions; matching keys stay where they are and are not compared.
min_keys int 2 Mappings with fewer keys are not checked.
blank_line_groups bool false Sort each run of entries between blank lines on its own.
path_patterns list [] Regular expressions; only mappings (sequences) whose key path matches one are checked. The path is a.b[0].c (["odd key"] for keys that are not identifiers), empty for the root; none listed means everywhere. A TOML table counts as its header key and an array table is not indexed.
has_properties list [] Only mappings that have all of these keys are checked.
report string "moved" Which keys are reported. moved: the fewest keys that would have to move (eslint-plugin-jsonc). misplaced: every key that sorts before one above it (yamllint).
Values: moved, misplaced

Enabled by the presets: yaml/yamllint.

Suppress one line with sudsy-disable-next-line sorted-keys in a comment of the file’s language.

The same check in other tools. full: Sudsy reports what the upstream rule reports; partial: only part of it, as the last column says.

Tool Rule Match What differs
Biome assist/source/useSortedKeys partial no groupByNesting; natural order is not guaranteed identical to Biome’s for non-ASCII keys (unverified)
eslint-plugin-jsonc jsonc/sort-keys partial one ordering per rule configuration (ESLint’s list of entries with different orders per path: use overrides per file); no per-key keyPattern orders and no ignore order type (ignored_keys is the closest); report positions when several equally short moves exist
graphql-eslint alphabetize partial Upstream picks the kinds to sort (fields, values, selections, variables, arguments, definitions) and a groups order; sorted-keys has no per-kind switch and does not sort definitions.
taplo reorder_keys partial Taplo sorts only keys not separated by blank lines; the rule sorts the whole table.
taplo reorder_inline_tables partial A lint rule, not a formatter option.
yamllint key-ordering partial no locale collation: yamllint’s global locale uses the C library’s collation; Sudsy offers compare: ordinal | case_insensitive | natural instead
yamllint key-ordering.ignored-keys full