Sort CSV-like words or lines in Visual Studio Code.
Smart Sort allows you to:
- Sort words separated by comma, tab, pipe (
|
), dots (.
) or space - Sort lines by comparing selected parts of them, or entire content of them
Smart Sort chooses how and what to sort according to the selection state so there are just 2 commands provided: one for sorting ascending order, one for sorting descending order. For default key bindings, see "Key bindings" section.
Smart Sort recognizes numeric values so 2
comes before 10
.
To sort words, just select some words and hit Ctrl+Alt+R (or Cmd+Ctrl+R on mac.)
Smart Sort automatically recognizes the word separator. In order of priority, supported separators are:
- Comma (
,
, U+0013) - Tab (U+0009)
- Pipe (
|
, U+007C) - Dot (
.
, U+007C) # See notes below - Space (
On sorting words, word separators among them will be normalized. Strictly writing, whitespace characters surrounding the first word separator will be treated as part of the separator and they will be used to separate sorted words.
Here are some example animations:
Note
Dots (periods) are used as separator only if there is just one selection range
and it contains no whitespaces.
This is designed for sorting CSS compound selectors
(sorting .foo.bar
will be .bar.foo
).
To sort lines, select multiple lines and hit Ctrl+Alt+R (or Cmd+Ctrl+R on mac.)
If you select portions of lines by using
multple selections (multi-cursor),
the lines which are touched by the selection range will be sorted by comparing
the selected parts.
This is useful if you want to sort on arbitrary column of visually aligned text
data such as output of
ps
command
or CSV data.
Here are some example animations:
- Sort visually aligned lines by specific "column"
- Sort lines by entire content (CSV colorized with
Rainbow CSV)
- Sort lines by comparing selected parts
If smartSort.preferWordSorting
is configured as true
, Smart Sort will sort
words instead of lines under the condition shown below:
- there is only one selection range (not using multi-cursor), AND
- both the start and end of the selection range are NOT placed at a beginning of a line (placed in a middle of a line or at the end of a line).
In this mode, sorted words will be reflowed. In other words, the original indentation and line widths will remain unchanged. (for example, the indentation characters of the second line is kept unchanged in the example animation below).
Note that if you place both the start and end of the selection range are placed
at a beginning of a line, you can always sort lines even if you set
true
to smartSort.preferWordSorting
.
Here is an example animation:
- Sorting
import
target in Julia language:
In this example, we don't need to care about where to insert a new target; just appending one and sorting them will move it to the right place.
Smart Sort provides the commands below:
smartSort.SortAscending
- Smart Sort: Sort (ascending)
smartSort.SortDescending
- Smart Sort: Sort (descending)
By default key bindings are automatically defined for those commands.
Smart Sort provides the default keybindings below:
- Ctrl+Alt+R (mac: Cmd+Ctrl+R)
- Smart Sort: Sort (ascending)
- Ctrl+Alt+Shift+R (mac: Cmd+Ctrl+Shift+R)
- Smart Sort: Sort (descending)
-
smartSort.preferWordSorting
-
Controls whether to sort words spread over the lines or not when multiple lines are selected by a single selection. Note that you can sort lines regardless of this configuration by selecting from a beginning of a line to a beginning of another line.
See explanation below for detail. (default:
false
)
-
-
smartSort.useDotAsWordSeparator
- Controls whether to use dots (periods) as word separator or not.
This behavior is available only if there is just one selection range and
it contains no whitespaces.
This is useful for sorting CSV compound selectors like
.foo.bar
. (default:true
)
- Controls whether to use dots (periods) as word separator or not.
This behavior is available only if there is just one selection range and
it contains no whitespaces.
This is useful for sorting CSV compound selectors like
Previously the name of this extension was "Stable Sort". Here is why I wanted and created an extension which uses stable sort algorithm.
Back when Visual Studio Code was version 1.27.2, it have used unstable sort algorithm. This means that sorting textually identical words or lines may change those order. This behavior will not be a problem in most cases since swapping those is not a change by all means.
Unfortunately, I encountered an exceptional case. In Japanese locale, an ASCII digit character and its counter part in fullwidth forms are treated as equal so the order of words like below changed every time I sort them:
2型糖尿病
2型糖尿病
This behavior was very annoying when I was compositing dictionary data since I cannot normalize entries by simple sorting. Obviously an extension which allows me to sort entries using stable sort algorithm solves the problem. So, I created one.