-
Notifications
You must be signed in to change notification settings - Fork 13k
Closed as duplicate of#18286
Labels
DuplicateAn existing issue was already createdAn existing issue was already created
Description
🔍 Search Terms
As you may know, JS sorts numbers in string order.
It is mentioned in the documentation: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort
The problem is, nobody expects this. But we can't and shouldn't change JS behaviour. But I think it is really necessary for people to get an error when they sort numbers.
This way, users can know the sort will work unexpectedly, they can convert to string before sorting, or they can convert to Float64Array.
To test:
const numbers: number[] = [16, 1, 0, 9, 100].sort(),
// Result sort will be:
[16, 1, 0, 9, 100]
// Expected:
[0, 1, 9, 16, 100]
✅ Viability Checklist
- This wouldn't be a breaking change in existing TypeScript/JavaScript code
- This wouldn't change the runtime behavior of existing JavaScript code
- This could be implemented without emitting different JS based on the types of the expressions
- This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
- This isn't a request to add a new utility type: https://github.com/microsoft/TypeScript/wiki/No-New-Utility-Types
- This feature would agree with the rest of our Design Goals: https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals
⭐ Suggestion
Error for number array sort
📃 Motivating Example
const numbers: number[] = [16, 1, 0, 9, 100].sort(),
// Result sort will be:
[16, 1, 0, 9, 100]
Expected:
[0, 1, 9, 16, 100]
💻 Use Cases
- What do you want to use this for?
- What shortcomings exist with current approaches?
- What workarounds are you using in the meantime?
olivermrose
Metadata
Metadata
Assignees
Labels
DuplicateAn existing issue was already createdAn existing issue was already created