question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

editor.action.sortLinesAscending has confusing sort order for symbols

See original GitHub issue

When sorting lines using editor.action.sortLinesAscending (“Sort Lines Ascending” via the Command Palette) the lines are sorted in a very strange order when the comparison involves a symbol character.

I would expect that . (period) would be sorted before _ (underscore) because in ASCII, period is 46 and underscore is 95.

However, vscode uses localeCompare as its sorting method (see sortLinesCommand.ts:76), which results in underscore being sorted before period. See the repro steps below for an example.

I saw a previous issue regarding this (#15516) but it was closed because the ASCII ordering was in fact correct. In the case I’ve outlined below, the ordering is not correct.

I’m not sure what the correct solution would be but I think that for ASCII symbols, ASCII ordering should be obeyed. I do realise that “Sort Lines Ascending” is an ambiguous term - ascending according to what criteria? - so perhaps the command could be renamed to something more specific, or you could provide different default sorting options.

  • VSCode Version: 1.22.1 Commit 950b8b0d37a
  • OS Version: Windows 10 Enterprise Version 1709 OS Build 16299.371

Steps to Reproduce:

  1. Copy the following into a new file:
a_b.txt
a_b_c.txt
  1. Highlight the file contents.

  2. Open the Command Palette and select “Sort Lines Ascending”.

Expected:

The file is sorted in the following order:

a_b.txt
a_b_c.txt

Actual:

The file is sorted in the following order:

a_b_c.txt
a_b.txt

Does this issue occur when all extensions are disabled?: Yes

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:30
  • Comments:9 (2 by maintainers)

github_iconTop GitHub Comments

7reactions
ayrtonmasseycommented, Apr 18, 2018

Thanks for your response - interesting that the editor uses the same sort as the UI!

I use the sort function most often to sort lines in code, which usually contain only ASCII characters. The issue I (personally) have with the current vscode sort function is that when I pipe the file content into other utilities, those utilities expect lines to be in ASCII order.

Regarding the sorting of files in vscode’s explorer, Windows explorer sorts the files as I would expect:

image

Sorting in ASCII order is beneficial as shorter filenames are placed before longer ones.

Since this is supposed to be a general sort & is also applied to UI elements, it’s difficult to come up with a one-size-fits-all solution. Personally, I would introduce two settings:

  • sorting.method (or similar) which allows the options:
    • ascii, for sorting code / lists of files.
    • localeCompare, for sorting general text which may contain unicode characters, etc.
  • sorting.caseSensitivity (or similar) which allows the options:
    • caseInsensitive, values converted to lowercase before comparison
    • caseSensitive, values compared without modification

Perhaps these settings could be separate from the sorting in the explorer, e.g. editor.sorting.method vs. explorer.sorting.method.

3reactions
benkimballcommented, May 8, 2018

I’m currently sorting a very long YAML file in order for it to pass yamllint configured to enforce lexical sorting of keys. This issue is of interest to me because currently Code’s idea of a lexical sort and yamllint’s idea differ in the area of underscores:

After Code editor.action.sortLinesAscending:

---
underscore_underscore: second
underscore: first

yamllint output:

3:1       error    wrong ordering of key "underscore" in mapping  (key-ordering)
Read more comments on GitHub >

github_iconTop Results From Across the Web

VS Code: Sort lines of code in Ascending (or Descending) Order
Select the code you wish to sort in Visual Studio Code. Then hit the key combination Ctrl+P and type the greater than sign...
Read more >
Is possible to change Visual Studio Code default explorer sort ...
A sub-ordering of files and folders will be added to v1.57. The setting: Explorer > Sort Order Lexicographic Options.
Read more >
Change order of symbols for «sort-lines
I am using «sort-lines» to sort includes in C/C++ files before saving. The problem that arises is that a headers with " symbols...
Read more >
SDetective-H8BPKW-N8XZWP.exe - Hybrid Analysis
This report has 14 indicators that were mapped to 15 attack techniques and 8 tactics. ... with a procedure lookup that resolve to...
Read more >
ThinkAutomation Help
Over 100 built-in actions are included, plus ThinkAutomation is extensible ... You can arrange the order of fields on the form using the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found