VS Code: Sort Lines and JSON object properties

avatar
Borislav Hadzhiev

Last updated: Apr 6, 2024
3 min

banner

# Sort lines (Ascending and Descending) in VS Code

To sort lines ascending or descending in VS Code:

  1. Select the lines.
  2. Press:
  • Ctrl + Shift + P on Windows and Linux
  • Command + Shift + P on macOS
Note: you can also press F1 to open the Command Palette.
  1. Type sort lines and select Sort Lines Ascending to sort the lines in ascending order (from A to Z) or Sort Lines Descending to sort the lines in descending order (from Z to A).

sort lines ascending descending

Here is a short clip that demonstrates how to sort in ascending order.

sort lines ascending

And here is a clip that shows how to sort the line in descending order.

sort lines descending

# Setting a keyboard shortcut for the Sort lines actions

If you need to set a keyboard shortcut for the sort lines actions:

  1. Press:
  • Ctrl + Shift + P on Windows and Linux.
  • Command + Shift + P on macOS.
Note: you can also press F1 to open the Command Palette.
  1. Type Keyboard Shortcuts and select Preferences: Open Keyboard Shortcuts.

preferences open keyboard shortcuts

  1. Search for sort lines and double-click on the Sort Lines Ascending and Sort Lines Descending options.

  2. Press the keys you want to bind the actions to and press Enter.

set keyboard shortcuts for sort lines actions

# Using an Extension to sort lines in VS Code

There is also a very popular Sort lines extension that you can use if you need more sorting options.

You can install the extension by:

  1. Clicking on Extensions in the left sidebar.
  • You can also open the Extensions menu by pressing:
    • Ctrl + Shift + X on Windows or Linux.
    • Command + Shift + X on macOS.
  1. Typing sort lines.

  2. Clicking on the Install button.

install sort lines extension

Once you install the extension:

  1. Select the lines.
  2. Press:
  • Ctrl + Shift + P on Windows and Linux.
  • Command + Shift + P on macOS.
Note: you can also press F1 to open the Command Palette.
  1. Type sort lines and select one of the many options.

select sorting option

# Sort JSON object properties in VS Code

To sort the properties of a JSON object:

  1. Press:
  • Ctrl + Shift + P on Windows and Linux.
  • Command + Shift + P on macOS.
Note: you can also press F1 to open the Command Palette.
  1. Type JSON sort and select JSON: Sort Document.

json sort document

Here is a short clip that demonstrates the process.

sort json object

# Setting a Keyboard shortcut for the JSON: Sort document action

If you need to set a keyboard shortcut for the JSON sort document action:

  1. Press:
  • Ctrl + Shift + P on Windows and Linux.
  • Command + Shift + P on macOS.
Note: you can also press F1 to open the Command Palette.
  1. Type Keyboard Shortcuts and select Preferences: Open Keyboard Shortcuts.

preferences open keyboard shortcuts

  1. Search for JSON sort and double-click on the JSON: Sort Document option.

  2. Press the keys you want to bind the actions to and press Enter.

set keyboard shortcut for json sort

# Sort JSON objects in VS Code using an extension

You can also use an extension to sort JSON objects.

  1. Click on Extensions in the left sidebar.
  • You can also open the Extensions menu by pressing:
    • Ctrl + Shift + X on Windows or Linux.
    • Command + Shift + X on macOS.
  1. Type Sort JSON objects.

install sort json objects extension

  1. Click on the Install button.

Make sure to install the Sort JSON objects extension from richie5um2.

To use the extension:

  1. Select a JSON object (the extension uses full lines, so ensure the selected lines are a valid JSON object).

  2. Press Ctrl + Shift + P (or Command + Shift + P on macOS).

Note: you can also press F1 to open the Command Palette.
  1. Type Sort JSON and run the command.

sort json using extension

I've also written an article on how to remove unused imports and sort imports in VS Code.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.