Skip to content

Sorting numbers should throw error #62455

Closed as duplicate
Closed as duplicate

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?

Activity

  1. kirkwaiblinger commented on Sep 16, 2025

    @kirkwaiblinger
  2. RyanCavanaugh commented on Sep 16, 2025

    @RyanCavanaugh
    Member

    #18286 tracks the most-likely fix for this

  3. sahin52 commented on Sep 16, 2025

    @sahin52
    Author

    Ryan Cavanaugh (@RyanCavanaugh) Thank you, I am closing the task

  4. locked as resolved and limited conversation to collaborators on Nov 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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