Robin Malfait cd0c308afe
Improve sorting candidates containing numbers (#13507)
* implement custom `compare` for sorting purposes

This `compare` function compares two strings. However, once a number is
reached the numbers are compared as actual numbers instead of the string
representation.

E.g.:

```
p-1
p-2
p-10
p-20
```

Will be sorted as expected in this order, instead of

```
p-1
p-10
p-2
p-20
```

---

This should also make suggestions in the vscode extension more logical.

* update tests to reflect order changes

* update changelog

* reset `i` correctly

This makes the code more correct _and_ improves performance because the
`Number(…)` will now always deal with numbers.

On the tailwindcss.com codebase, sorting now goes from `~3.29ms` to
`~3.10ms`

* drop unreachable code

In this branch, it's guaranteed that numbers are _different_ which means
that they are never going to be the same thus unreachable code.

When we compare two strings such as:

```
foo-123-bar
foo-123-baz
```

Then all characters until the last character is the same character in
both positions. This means that "numbers" that are the same in the same
position will be compared as strings instead of numbers. But that is
fine because they are the same anyway.

* add fallback in case numbers are the same but strings are not

This can happen if we are sorting `0123` and `123`. The `Number`
representation will be equal, but the string is not.

Will rarely or even never happen. But if it does, this makes it
deterministic.

* re-word comment

* add more test cases with numbers in different spots with various lengths

* Update CHANGELOG.md

* cleanup, simplify which variables we increment

This also gets rid of some explanation that can now be omitted entirely.

---------

Co-authored-by: Adam Wathan <adam.wathan@gmail.com>
2024-04-15 17:56:30 +02:00
..
2024-04-09 20:55:54 +02:00
2024-04-09 20:55:54 +02:00
2024-03-05 14:29:15 +01:00