Tyriar / vscode-sort-lines

Visual Studio Code extension to sort lines of text
https://marketplace.visualstudio.com/items/Tyriar.sort-lines
MIT License
151 stars 44 forks source link

Feature request: A combination of natural and lexicographic sort #103

Open NotWearingPants opened 3 years ago

NotWearingPants commented 3 years ago

I have a list of non-whole numbers. This is the correct ordering:

2.4
10.13
10.2

Sorting using VS Code's sort gives:

10.13
10.2
2.4

Sorting with Sort lines (natural) gives:

2.4
10.2
10.13

And they're both wrong 😧

The problem here is that for the number before the decimal point I need a natural sort, and then for the number after the decimal point I need a normal lexicographic sort.

This is also be a problem with lists of version numbers (which can also have more than one "decimal" point). But in this case I think it's only about leading zeros. This is an example of a correct ordering:

1.03.0
1.2.0
1.10.0

Leading zeros (for parts other than the initial number before the dot) do mean that a number is less significant, but otherwise this is a natural sort rather than a lexicographic.


These look like two variants of "natural" sort, which come in handy in different situations. So ideally they should be separate commands rather than settings (but also there are too many commands so idk).

mrienstra commented 1 year ago

That would be useful!

This Python library has an implementation: https://github.com/SethMMorton/natsort#sorting-by-real-numbers-ie-signed-floats

Drilling down to the underlying regex: natsortednatsort_keygenregex_chooserfloat related NumericalRegularExpressions

Alas, it would add quite a bit of complexity, at the moment this project uses an Intl.Collator with the numeric option (example), see src/sort-lines.ts#L95-L100

Using natsort under the hood could be an option, it looks do-able, see https://stackoverflow.com/questions/67972473/how-to-make-a-vs-code-extension-in-python

Thinking about this more broadly -- i.e. outside of the scope of this issue -- it would be lovely to have a natural sort algorithm that supported floating point numbers by default, but obviously that's not possible, there's too many situations in which it would be ambiguous, e.g. 1.11 could be a semver without a specified patch or a floating point number. In the context of a GUI editor like VS Code, it would be nice to be prompted when there's ambiguity like that, with some sample lines shown.