Skip to content

Commit b48dc0b

Browse files
committed
doc,test: fix inspect's sorted compare function
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. Backport-PR-URL: #23039 PR-URL: #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]>
1 parent 68eaa87 commit b48dc0b

File tree

2 files changed

+2
-2
lines changed

2 files changed

+2
-2
lines changed

doc/api/util.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -554,7 +554,7 @@ const o1 = {
554554
};
555555
console.log(inspect(o1, { sorted: true }));
556556
// { a: '`a` comes before `b`', b: [ 2, 3, 1 ], c: Set { 1, 2, 3 } }
557-
console.log(inspect(o1, { sorted: (a, b) => a < b }));
557+
console.log(inspect(o1, { sorted: (a, b) => b.localeCompare(a) }));
558558
// { c: Set { 3, 2, 1 }, b: [ 2, 3, 1 ], a: '`a` comes before `b`' }
559559

560560
const o2 = {

test/parallel/test-util-inspect.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1730,7 +1730,7 @@ assert.strictEqual(
17301730
assert.strictEqual(
17311731
inspect(
17321732
{ a200: 4, a100: 1, a102: 3, a101: 2 },
1733-
{ sorted(a, b) { return a < b; } }
1733+
{ sorted(a, b) { return b.localeCompare(a); } }
17341734
),
17351735
'{ a200: 4, a102: 3, a101: 2, a100: 1 }'
17361736
);

0 commit comments

Comments
 (0)