Last updated: Apr 6, 2024
Reading time·3 min
To sort lines ascending or descending in VS Code:
Ctrl
+ Shift
+ P
on Windows and LinuxCommand
+ Shift
+ P
on macOSF1
to open the Command Palette.Here is a short clip that demonstrates how to sort in ascending order.
And here is a clip that shows how to sort the line in descending order.
If you need to set a keyboard shortcut for the sort lines actions:
Ctrl
+ Shift
+ P
on Windows and Linux.Command
+ Shift
+ P
on macOS.F1
to open the Command Palette.Search for sort lines and double-click on the Sort Lines Ascending and Sort Lines Descending options.
Press the keys you want to bind the actions to and press Enter
.
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:
Ctrl
+ Shift
+ X
on Windows or Linux.Command
+ Shift
+ X
on macOS.Typing sort lines.
Clicking on the Install button.
Once you install the extension:
Ctrl
+ Shift
+ P
on Windows and Linux.Command
+ Shift
+ P
on macOS.F1
to open the Command Palette.To sort the properties of a JSON object:
Ctrl
+ Shift
+ P
on Windows and Linux.Command
+ Shift
+ P
on macOS.F1
to open the Command Palette.Here is a short clip that demonstrates the process.
If you need to set a keyboard shortcut for the JSON sort document action:
Ctrl
+ Shift
+ P
on Windows and Linux.Command
+ Shift
+ P
on macOS.F1
to open the Command Palette.Search for JSON sort and double-click on the JSON: Sort Document option.
Press the keys you want to bind the actions to and press Enter
.
You can also use an extension to sort JSON objects.
Ctrl
+ Shift
+ X
on Windows or Linux.Command
+ Shift
+ X
on macOS.Make sure to install the
Sort JSON objects
extension from richie5um2
.
To use the extension:
Select a JSON object (the extension uses full lines, so ensure the selected lines are a valid JSON object).
Press Ctrl
+ Shift
+ P
(or Command
+ Shift
+ P
on macOS).
F1
to open the Command Palette.I've also written an article on how to remove unused imports and sort imports in VS Code.
You can learn more about the related topics by checking out the following tutorials: