Skip to content

sgryjp/vscode-stable-sort

Repository files navigation

Smart Sort

Visual Studio Marketplace Version Visual Studio Marketplace Rating (Stars) Visual Studio Marketplace Installs   CI status MIT license

Sort CSV-like words or lines in Visual Studio Code.

Summary

Smart Sort allows you to:

  1. Sort words separated by comma, tab, pipe (|), dots (.) or space
  2. 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.

Feature

Sorting words

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:

  1. Comma (,, U+0013)
  2. Tab (U+0009)
  3. Pipe (|, U+007C)
  4. Dot (., U+007C) # See notes below
  5. Space ( , U+0020)

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:

  • Comma
    Sorting words separated by comma
  • Tab
    Sorting words separated by tab
  • Pipe
    Sorting words separated by pipe
  • Space
    Sorting words separated by space

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).

Sorting lines

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:

  1. Sort visually aligned lines by specific "column"
    Sorting lines by selected parts
  2. Sort lines by entire content (CSV colorized with Rainbow CSV)
    Sorting lines by entire content
  3. Sort lines by comparing selected parts
    Sorting lines by selected parts

Sorting words spread over multiple line

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:
    Sorting words spread over multiple lines
    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.

Contribution Points

Commands

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.

Key bindings

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)

Configuration

  • 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)

Historical Background

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.