Skip to content

Commit e758d4a

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. PR-URL: #22992 Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Kyle Farnung <kfarnung@microsoft.com>
1 parent 36bdd19 commit e758d4a

File tree

2 files changed

+2
-2
lines changed

2 files changed

+2
-2
lines changed

doc/api/util.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -556,7 +556,7 @@ const o1 = {
556556
};
557557
console.log(inspect(o1, { sorted: true }));
558558
// { a: '`a` comes before `b`', b: [ 2, 3, 1 ], c: Set { 1, 2, 3 } }
559-
console.log(inspect(o1, { sorted: (a, b) => a < b }));
559+
console.log(inspect(o1, { sorted: (a, b) => b.localeCompare(a) }));
560560
// { c: Set { 3, 2, 1 }, b: [ 2, 3, 1 ], a: '`a` comes before `b`' }
561561

562562
const o2 = {

test/parallel/test-util-inspect.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1688,7 +1688,7 @@ assert.strictEqual(
16881688
assert.strictEqual(
16891689
inspect(
16901690
{ a200: 4, a100: 1, a102: 3, a101: 2 },
1691-
{ sorted(a, b) { return a < b; } }
1691+
{ sorted(a, b) { return b.localeCompare(a); } }
16921692
),
16931693
'{ a200: 4, a102: 3, a101: 2, a100: 1 }'
16941694
);

0 commit comments

Comments
 (0)