All Tools View Categories About Contact Privacy

Sort YAML

undefined

Runs in your browser. The YAML text is processed locally and is not sent to an external service.

About Sort YAML

Sorting YAML is mainly a consistency task: it helps a configuration file follow a predictable order so people can scan it quickly and code reviews contain fewer formatting-only changes. This tool parses the document first, then rebuilds it according to the selected strategy. In key mode, mapping keys are ordered alphabetically at every level, so a top-level file and its nested sections receive the same rule. In value mode, mapping entries are ordered using the rendered scalar value and primitive arrays are sorted as well. Complex arrays are deliberately left alone because comparing whole objects would require a policy that YAML itself does not define.

The important learning point is that sorting is a transformation of the YAML data model, not a source-text operation. Comments, original whitespace, and quote choices are not preserved because the file is parsed and serialized again. That is useful when your objective is canonical ordering, but it would be the wrong tool for a documentation file where comments carry operational meaning. A good workflow is therefore to keep source comments in version control, run this tool when you need a clean data-oriented representation, and review the resulting diff before replacing a managed configuration file.

Key ordering is especially valuable for generated fixtures and configuration catalogs. Suppose two environments contain the same fields but in different orders. Sorting both files can make a visual comparison much easier without changing the underlying values. It is also useful when teaching YAML: predictable key order lets a learner focus on hierarchy and values rather than wondering why equivalent examples look different. Value sorting is more specialized. It can make lookup-style maps easier to scan, but it should not be applied blindly to arrays where sequence order has meaning, such as middleware chains, firewall rules, deployment steps, or priority lists.

Before sorting a production document, validate it and keep the original available. A malformed file is rejected here so you do not get a partially transformed result. The output should be treated as a new representation, especially when anchors, comments, or advanced YAML presentation features matter to your workflow. For normal configuration data, the result is deterministic: the same input and mode produce the same ordering, which makes the tool useful for repeatable examples, snapshots, and test fixtures.

For a practical exercise, start with the sample and sort by keys. Notice how alpha, items, and zeta move while nested fields are ordered too. Then switch to value mode and observe that the simple list changes order. Try a list containing objects and notice that complex elements are intentionally retained. This distinction is a useful lesson when designing configuration conventions: sort the structures where order is descriptive, but preserve sequence where order is behavior. The browser-only implementation also means sensitive configuration stays in the current session rather than being posted to a conversion service.

For team conventions, decide whether order is meaningful before adopting sorting in a build step. Key order is normally cosmetic for mappings, but list order can be operational. If you automate the transformation, place it before review and after validation, and keep generated output separate from hand-written source when comments matter. A useful test is to parse both the original and sorted documents and assert that their data models are equal. That turns formatting policy into a repeatable check rather than a matter of taste.

Sorting by values should be treated as a specialized convenience rather than a universal canonical format. A map of country codes or display labels can benefit from value order; a priority map might not. When reviewers need to understand why an entry moved, the normalized output is easier to explain when the ordering rule is documented alongside the repository's style guide.

A final review habit is to look at semantic order separately from visual order. If the sorted file is intended for humans, explain the chosen rule in the repository so future contributors know why a key moved. If it is generated output, test equality after parsing rather than comparing raw text. That keeps the check focused on the real contract: the data should remain the same while the representation becomes more predictable.

Features

  • Feature 1: Sort mapping keys alphabetically at every nesting level.
  • Feature 2: Sort primitive arrays when using value mode.
  • Feature 3: Sort object entries by their scalar values in value mode.
  • Feature 4: Preserve nested objects and sequence structure.
  • Feature 5: Keep the operation local in the browser.
  • Feature 6: Reject malformed YAML before changing it.
  • Feature 7: Show a compact before/after statistic.
  • Feature 8: Provide copy and download output controls.

How to Use

  1. Paste or type the YAML source in the editor.
  2. Use Load Sample to see a representative input.
  3. Choose whether to sort by keys or by values.
  4. Click Sort YAML and inspect the regenerated document.
  5. Use Copy for the clipboard or Download for a local file.
  6. Keep the original YAML when exact comments and formatting need to be preserved.

Examples

  • Key sorting: zeta: 3 moves after items and alpha.
  • Value sorting: a list containing alpha, bravo, zulu becomes alphabetic.
  • Nested maps: child keys are sorted as well.
  • Complex arrays: arrays of objects retain sequence order.
  • Review fixture: canonical ordering can reduce formatting noise.

Benefits

  • Practical: Make generated configuration easier to scan.
  • Reviewable: Reduce noisy key ordering changes in reviews.
  • Local: Prepare stable fixtures for tests and examples.
  • Reusable: Group related settings predictably.
  • Maintainable: Inspect value ordering in simple lookup maps.
  • Fast: Standardize configuration files before documentation.

Frequently Asked Questions

Does sorting change YAML values?
Key mode changes mapping order only. Value mode orders primitive lists and object entries by their rendered scalar value, while nested data remains intact.
Are arrays always sorted?
Only primitive arrays are sorted in value mode. Arrays containing mappings or other complex objects keep their original order because a natural value order would be ambiguous.
Are comments preserved?
No. Sorting parses the YAML data and writes a fresh YAML representation, so comments and original spacing are not preserved.
Will quoted strings stay quoted?
The output is regenerated from the parsed value model, so quoting may change when it is not required for validity.
Can I sort by nested keys?
Yes. The key-order operation is applied recursively through nested mappings.
What happens with invalid YAML?
The tool stops and reports a parser error instead of sorting incomplete data.
Is the original file uploaded?
No. The conversion happens in the browser using the pasted text.
Can I save the sorted result?
Yes. Copy places the output on the clipboard and Download saves a local YAML file.