// formatters
YAML Key Sorter.
Recursively sort YAML keys alphabetically while preserving arrays and scalars. Useful for diff-friendly files.
YAML input
Sorted output
Why sort YAML keys?
Sorted keys make YAML files easier to scan, diff, and review. When keys are in alphabetical order, you can find any field instantly without scanning the whole document. In version control, sorted keys produce smaller, cleaner diffs — adding a new key only shows one line changed instead of every key after the insertion point shifting.
This is especially valuable in large config files like Kubernetes manifests, Docker Compose stacks, or Ansible playbooks where a single file might have 50+ keys across nested objects. Sorting brings predictability: every contributor's file looks the same regardless of the order they added fields.
How the sort works.
The tool parses your YAML into a JavaScript object, recursively sorts every mapping's keys alphabetically (case-insensitive), and dumps the result back to YAML. Sequences (arrays) preserve their original order — only mappings are sorted. Nested objects are sorted at every level, so service → name → version becomes service → name → replicas → version.
If your YAML has syntax errors, the tool displays the parse error with the line number. Fix the error in the input pane and the sorted output will update automatically. The tool also preserves YAML comments that appear on their own line, but inline comments may be lost during the parse-and-dump cycle.
When to sort keys.
Before committing. Run your YAML through this tool before committing to version control. Consistent key order eliminates noise in code reviews and makes git blame more useful.
Config standardization. When multiple team members contribute to the same config files, enforce a sorted-key convention to avoid merge conflicts and keep files readable.
API responses. If your API serializes data as YAML, sorting keys makes the output deterministic — the same data always produces the same YAML, which is useful for snapshot testing and caching.
How sorted keys improve diffs.
When you add a key to an unsorted YAML file, every key below the insertion point shifts, creating a diff that shows multiple lines changed even though only one line was added. With sorted keys, the new line appears in its correct alphabetical position and only that line shows as changed. This makes code reviews faster because reviewers can focus on the actual change instead of parsing noise.
This benefit compounds in files with many contributors. A Kubernetes deployment manifest with 30 keys where every contributor adds fields in different positions creates a tangled git history. Sorting keys before committing keeps the history linear and each commit focused on its actual change.
Key ordering and merge conflicts.
Merge conflicts in YAML are often caused by different contributors inserting keys at different positions in the same mapping. Git sees adjacent lines as conflicting even when the changes are logically independent. Sorting keys eliminates this class of conflict because each key has a fixed position regardless of who added it.
In a CI/CD pipeline, you can enforce sorted keys automatically with a pre-commit hook or a CI check that runs this tool on all YAML files. If the output differs from the input, the check fails and the contributor is asked to re-sort before pushing. This keeps the entire repository consistent without manual intervention.
Sorting in automation workflows.
If you manage infrastructure with tools like Ansible, Terraform, or Helm, you likely have dozens or hundreds of YAML files. Sorting keys across all of them produces a uniform structure that's easier for both humans and machines to parse. Tools that diff YAML files — such as yamldiff or CI-based config validators — work more reliably when the input has a consistent key order.
For teams adopting GitOps workflows, sorted YAML is especially valuable. When Flux or Argo CD reconciles desired state against live state, it compares YAML documents. Deterministic key order ensures the comparison is meaningful and doesn't flag cosmetic differences as drift.
FAQ
How do I alphabetically sort keys in a YAML file?
Use `yq -P sort_keys=true file.yaml` to dump with keys sorted alphabetically, or `yaml-sort` (npm package) for in-place sorting that preserves comments.
Will sorting YAML keys break my anchors and references?
Yes, naive alphabetical sorting can break `&anchor` / `*reference` pairs if anchors and their references are in the same mapping — use a sort tool that understands YAML structure.
How do I sort Kubernetes YAML manifests in a specific order (kind, name)?
Use `yq` with `sort_keys` and `sort_by`, or use the `kubesort` tool that understands k8s manifest structure (apiVersion, kind, metadata, spec, status).
Can I sort YAML keys while keeping comments in place?
Yes — `yaml-sort` (Node) and `prettier` both preserve comments when reordering keys; `yq` does NOT preserve comments by default.
Why does sorting YAML change my file semantically?
It shouldn't — if it does, you have duplicate keys (last one wins) or anchors that depend on position; sort, then verify with a diff tool like `dyff`.