New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Normative] Make Array.prototype.sort stable #1340

Open
wants to merge 1 commit into
base: master
from

Conversation

Projects
None yet
8 participants
@mathiasbynens
Member

mathiasbynens commented Nov 2, 2018

All major JavaScript engines now have a stable Array.prototype.sort implementation. Let’s update the spec accordingly.

References:

@mathiasbynens

This comment has been minimized.

Member

mathiasbynens commented Nov 2, 2018

Added to November agenda: tc39/agendas@4fdf671

@mathiasbynens

This comment has been minimized.

Member

mathiasbynens commented Nov 27, 2018

This got consensus at today’s TC39 meeting. 🎉

mathiasbynens added a commit to mathiasbynens/test262 that referenced this pull request Nov 29, 2018

mathiasbynens added a commit to mathiasbynens/test262 that referenced this pull request Nov 29, 2018

@mathiasbynens

This comment has been minimized.

Member

mathiasbynens commented Nov 29, 2018

Test262 tests: tc39/test262#1977

mathiasbynens added a commit to mathiasbynens/test262 that referenced this pull request Nov 30, 2018

@littledan littledan added has tests and removed needs tests labels Dec 6, 2018

@littledan

This comment has been minimized.

Member

littledan commented Dec 6, 2018

Since this has tests and consensus, is it ready to merge?

@ljharb

This comment has been minimized.

Member

ljharb commented Dec 6, 2018

The tests aren’t merged yet.

leobalter added a commit to tc39/test262 that referenced this pull request Dec 6, 2018

@mathiasbynens

This comment has been minimized.

Member

mathiasbynens commented Dec 6, 2018

@ljharb They are now!

@ljharb ljharb requested review from ljharb, bterlson, zenparsing and tc39/ecma262-editors Dec 6, 2018

@ljharb

ljharb approved these changes Dec 6, 2018

@mathiasbynens mathiasbynens force-pushed the mathiasbynens:sort-stability branch from cca5a66 to a85c9d9 Dec 6, 2018

@8eecf0d2

This comment has been minimized.

8eecf0d2 commented Dec 7, 2018

I know it's not my place, but lgtm!

@Yaffle

This comment has been minimized.

Contributor

Yaffle commented Dec 8, 2018

Is the spec for TypedArray#sort still points to the Array#sort? I am not fan of TypedArrays at all, but ... this changes also makes TypedArray#sort stable, which is not true in engines.

@mathiasbynens

This comment has been minimized.

Member

mathiasbynens commented Dec 10, 2018

@Yaffle Yeah, %TypedArray%.prototype.sort indeed refers to Array.prototype.sort.

this changes also makes TypedArray#sort stable, which is not true in engines.

Can you elaborate? How is stability even observable for typed arrays (since they can only contain primitives)?

@anba

This comment has been minimized.

Contributor

anba commented Dec 10, 2018

It's observable with a custom comparator function: https://bugzilla.mozilla.org/show_bug.cgi?id=1290554 (only the first test case, the second one is SpiderMonkey-specific).

@Yaffle

This comment has been minimized.

Contributor

Yaffle commented Dec 10, 2018

Custom comparator, which ignores some bits...

const size = 32;
const array = new Uint32Array(size);
for (let i = 0; i < size; i++) {
  array[i] = (i % 3) * 1000 + i;
}
let comparefn = (a, b) => Math.floor(a / 1000) - Math.floor(b / 1000);

console.log('Array#sort', Array.prototype.sort.call(array.slice(0), comparefn).join(' '));
console.log('TypedArray#sort', array.slice(0).sort(comparefn).join(' '));
@mathiasbynens

This comment has been minimized.

Member

mathiasbynens commented Dec 10, 2018

Looks like only V8 (issue) and SpiderMonkey (issue) have unstable %TypedArray%.prototype.sort implementations.

To unblock this PR, I'll amend the patch to move the previous non-stable warning to the TypedArray section. I'm hopeful that we'll be able to add the stability guarantee to %TypedArray%.prototype.sort in the spec as well.

@mathiasbynens mathiasbynens force-pushed the mathiasbynens:sort-stability branch from a85c9d9 to 2624806 Dec 10, 2018

@ljharb

ljharb approved these changes Dec 10, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment