Skip to content

Commit

Permalink
doc,test: fix inspect's sorted compare function
Browse files Browse the repository at this point in the history
In V8 7.0, the array sorting algorithm was changed to Timsort, which
is stable. A compare function returning only `true` or `false`
(converted to 0 and 1) cannot work properly.

PR-URL: nodejs#22992
Reviewed-By: Ruben Bridgewater <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Kyle Farnung <[email protected]>
  • Loading branch information
targos committed Sep 22, 2018
1 parent 36bdd19 commit e758d4a
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 2 deletions.
2 changes: 1 addition & 1 deletion doc/api/util.md
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,7 @@ const o1 = {
};
console.log(inspect(o1, { sorted: true }));
// { a: '`a` comes before `b`', b: [ 2, 3, 1 ], c: Set { 1, 2, 3 } }
console.log(inspect(o1, { sorted: (a, b) => a < b }));
console.log(inspect(o1, { sorted: (a, b) => b.localeCompare(a) }));
// { c: Set { 3, 2, 1 }, b: [ 2, 3, 1 ], a: '`a` comes before `b`' }

const o2 = {
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-util-inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -1688,7 +1688,7 @@ assert.strictEqual(
assert.strictEqual(
inspect(
{ a200: 4, a100: 1, a102: 3, a101: 2 },
{ sorted(a, b) { return a < b; } }
{ sorted(a, b) { return b.localeCompare(a); } }
),
'{ a200: 4, a102: 3, a101: 2, a100: 1 }'
);
Expand Down

0 comments on commit e758d4a

Please sign in to comment.