Docs/Sort
Sort JSON keys and arrays
Sort orders the keys of an object, the values of an array, or an array of objects by one of their fields, in either direction.
Sort a part of the document
- Select the object or array in tree mode, or leave nothing selected.
- Press the Sort button in the panel’s toolbar, or press Ctrl+Alt+S.
- Check What to sort: it starts with your selection, or the first array of objects.
- Pick the field and the direction. The dialog shows the new order before you press Sort.
Keys
Sort a JSON array: an example
To sort a JSON array, select it in tree mode, press Sort, pick what to sort by and the direction, then press Sort in the dialog. It works on a list of records sorted by one of their fields and on a plain list of numbers or text.
For example, the array of objects [{"name":"Lamp","price":35},{"name":"Mug","price":8},{"name":"Desk","price":120}] sorted by the field price, ascending, becomes [{"name":"Mug","price":8},{"name":"Lamp","price":35},{"name":"Desk","price":120}]. Each item moves as a whole, so every name stays with its price.
An array of values sorts by the values themselves: [10,2,33,4] ascending becomes [2,4,10,33], as numbers rather than as text. A list of text such as ["item 10","item 2","Item 1"] becomes ["Item 1","item 2","item 10"]: capital letters do not change the order, and numbers inside the text count as numbers.
Sort JSON keys alphabetically
To put the keys of a JSON object in A to Z order, select the object in tree mode and open Sort. Sort by is set to Key name; pick Ascending for A to Z or Descending for Z to A, then press Sort.
Each value moves with its key, and nothing else in the document changes. Key sort orders the keys of the object you chose; objects nested inside it keep their own order, so select a nested object and sort it too when you need its keys in order.
Key names compare without regard to capital letters, and numbers inside them count as numbers, so step2 comes before step10.
Three kinds of sort
- An object is sorted by its key names.
- An array of objects is sorted by a field of each item, such as
radiusKm. Nested fields work too, such asaddress.city. - An array of values is sorted by the values themselves.
How values are ordered
Numbers compare by their exact value, even past what a calculator can hold. Text compares the way people read it, so item 2 comes before item 10.
Values of different types keep a fixed order: null, then true and false, numbers, text, arrays and objects. Items that don’t have the field stay at the end in both directions.
Sorting keeps equal items in their old order. One Undo returns the whole part to how it was. In table mode, clicking a column title sorts by that column.
When sorting helps
- Two versions of a document are easier to read side by side when their keys are in the same order; see JSON diff: compare two JSON files.
- Sort a list of records by a field, such as a name or a price, before you export it, so the rows of the CSV come out in that order; see JSON to CSV: export and import data.
- For a sorted copy of only some items or fields, use a query instead; see Query and transform JSON. To lay the result out with your indentation, see JSON formatter and JSON repair.
- Sorting runs in your browser, like every tool here, so the JSON you sort is not sent to a server.