YAML Key Sorter
Sort YAML keys alphabetically, with an option to recurse into nested maps.
Original YAML
Sorted YAML
How it works: Keys are sorted alphabetically (case-insensitive). Nested objects are also sorted recursively.
Features
- Sort YAML keys alphabetically (ascending or descending)
- Recursive key sorting for nested objects
- Preserve comment positions (best-effort)
- Preview sorted output before applying
- Configurable: sort only top-level keys, or recursively sort all levels
- Merge with formatter for clean sorted output
Common Use Cases
- Standardize key ordering across config files for predictable diffs
- Sort Kubernetes manifest fields to match CRD documentation order
- Reduce merge conflicts by keeping keys in a consistent order
- Improve readability of large YAML files by grouping related alphabetically-adjacent keys
- Enforce alphabetical ordering in CI as part of a style guide
Why Sort YAML Keys?
YAML mappings (objects) have no inherent ordering β the specification says key order is implementation-defined. In practice, parsers usually preserve insertion order, which means manually edited files develop arbitrary key orderings over time.
Sorting keys alphabetically provides two benefits: it makes code reviews cleaner (you can tell at a glance if a key was added or removed at the right alphabetical position) and it reduces merge conflicts (when two developers add different keys, they won't conflict if both follow the same ordering rule).
The tradeoff is that alphabetical order may not reflect logical grouping. Use judgment: alphabetical ordering makes most sense for configuration dictionaries and less sense for step-by-step pipeline stages.
Examples
version: 1
name: app
author: Alice
desc: My Appauthor: Alice
desc: My App
name: app
version: 1Frequently Asked Questions
For most use cases, no β YAML mappings are interpreted as unordered dictionaries by applications. However, there are exceptions: some tools process YAML steps or stages in document order (e.g., CI pipeline steps). Always test after sorting if your YAML represents ordered workflows.
Comments are associated with the key that follows them. The sorter attempts to move inline and preceding comments with their associated key. However, freestanding block comments between keys may be repositioned or lost. Review the output carefully if comments are important.
It depends on the section. Fields like metadata, spec, status follow a conventional order that matches the Kubernetes documentation. Sorting these alphabetically would deviate from convention. However, within labels, annotations, or env maps, alphabetical sorting is common and helpful.
π‘ Tips
- Combine with the YAML Formatter to get both sorted and consistently indented output in one step.
- Use the diff view to inspect exactly which keys moved β this helps you sanity-check the sorted output.
- Consider excluding <code>steps</code>, <code>stages</code>, or <code>containers</code> arrays from recursive sorting, since their order is semantically significant.