Skip to content

sort/sortBy Does Not Use The Correct Ordering & Violates Parametricity #155

Closed
@eric-corumdigital

Description

@eric-corumdigital

All undefined elements are sorted to the end of the array, with no call to compareFunction.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort#Description

Therefore, sorting on any type inhabited by undefined will not use the correct ordering (except by coincidence). This is also a parametricity violation.

The only truthful use of native sort is on JavaScript values.

I propose we replace the implementation with a correct one. Yes, it will be slower. We might define unsafeSort to use the native sort with the same typing as sort. Then we may also have a different sort definition typed on JavaScript values (which is Foreign).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions