Open NotWearingPants opened 3 years 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: natsorted
➝ natsort_keygen
➝ regex_chooser
➝ float 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.
I have a list of non-whole numbers. This is the correct ordering:
Sorting using VS Code's sort gives:
Sorting with
Sort lines (natural)
gives: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:
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).