Sort object keys alphabetically in YAML documents

Sort YAML mapping keys recursively without changing sequence order.

freeworks offlinenothing uploaded
ToolYAML Sort Keys
Input
Output
Put this on your own site

The frame below runs the same code as this page, in the reader's own browser. Nothing is sent to us, and nothing is sent to you.

Pick a dark background and the text and panels follow it, so the frame stays readable on a dark page.

Preview

How it works

Mappings are traversed and keys sorted lexically in ascending or descending order, while sequences retain their order and nested mappings are sorted. The resulting values are serialized back to YAML.

  • Ascending order makes diffs stable.
  • Sequence order remains data.

Worked example

Alphabetize nested config keys
Sort all keys recursively in a multi-section config for consistent ordering
Input
											server:
  port: 3000
  host: localhost
database:
  port: 5432
  host: db.example.com
  name: myapp
app:
  name: MyApp
  version: 1.0.0
										
Output
												app:
  name: MyApp
  version: 1.0.0
database:
  host: db.example.com
  name: myapp
  port: 5432
server:
  host: localhost
  port: 3000

											

When to use this

Kubernetes values, CI snapshots, and generated artifacts sort YAML keys.

Edge cases

  • Case-sensitive ordering may differ from locale expectations.
  • Array items do not move.
  • Anchors and aliases may be expanded or rewritten by serialization.

References