Skip to content

Sorting numbers should throw error #62455

@sahin52

Description

@sahin52

🔍 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.

Related issues:
#58160
#32593

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

⭐ 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

  1. What do you want to use this for?
  2. What shortcomings exist with current approaches?
  3. What workarounds are you using in the meantime?

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions