Sort CSV-like words or lines in VS Code using stable sort algorithm.
With single shortcut Ctrl+Alt+R (mac: Cmd+Ctrl+R), you can:
If you want to sort in reversed (descending) order, use Ctrl+Alt+Shift+R (mac: Cmd+Ctrl+Shift+R).
Some other key ponits:
If there is only one selection range and either start position, end position or both are in the middle of a line, words inside the selection. You can select words across lines; in that case the original indentation and line widths will be kept.
Word separators will be recognized automatically. If one of comma (
Note that spaces surrounding word separators will be normalized as below:
If the condition to sort words are not met, selected lines will be sorted.
Strictly writing, lines which contains selection range(s) will be sorted using
selected part. This means that you can sort lines not only by comparing entire
content but also by comparing a portion of them. This is useful if you want to
sort on arbitrary column of visually aligned text data such as output of
Sorting Words Spread over Multiple Lines
By default you cannot sort words spread over multiple lines. If the selection
covers multiple lines, those lines touched by the selection will be sorted.
To change this behavior, set
Stability of sorting is not valuable in most cases except for some.
As of VS Code 1.27.2, its algorithm to sort lines are not "stable" so sorting textually "same" words or lines may change those order. This behavior will not be a problem in most cases because those are normally completely identical so swapping those is not a change by all means. But, there are exceptional cases where two textually differrent lines are evaluated as equal. In those cases, sorted result will be different time to time.
For example, an ASCII digit character and its counter part in fullwidth forms are treated as equal in Japanese locale so result of sorting words like below (BTW it's "type 2 diabetes" writen in Japanese) may change time to time:
This behavior is not appreciated for tasks like compositing dictionary data, since you cannot normalize data by simply sorting. This extension solves the problem.
(Note that the "unstable" sort algorithm of VS Code (strictly, of Node.js) is used for relatively large number of lines so working with small data might not raise the problem.)