SAA-2291: Introduce convention for strings with number prefix #1108
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Identify the bug
#1107
Description of the change
Instead of running all cell values through
parseFloat
, which converts strings with number prefixes to just the number, we useNumber
to evaluate the entire value to see whether it's a number or not. If the value is NaN, the function returns the original value (this includes strings with number prefixes) for use in the sort. If the value is a number, we can then safely useparseFloat
on the sort value, as we know it won't be removing any non-numerical components of the value.Alternative designs
Number
evaluates the entire value, whereasparseFloat
stops when you get to a non-numerical part.You could use
parseFloat
and then check the result against the original input value, and use the original value if it is different from the result of theparseFloat
, but this is more long-winded and potentially error-prone.Possible drawbacks
Number
converts empty strings andnull
to 0. I think that this works fine when sorting - if I had a cell which looked empty, I'd rather it was at the bottom of the table rather than at the top - but this depends on your point of view.Verification process
Verified against existing tests and introduced a new test with a variety of input values.
Release notes