From e758d4ab0adf53f2a4b736433714943bc4b4f06f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Zasso?= Date: Fri, 21 Sep 2018 10:40:42 +0200 Subject: [PATCH] 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: https://github.com/nodejs/node/pull/22992 Reviewed-By: Ruben Bridgewater Reviewed-By: Colin Ihrig Reviewed-By: James M Snell Reviewed-By: Kyle Farnung --- doc/api/util.md | 2 +- test/parallel/test-util-inspect.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/api/util.md b/doc/api/util.md index f4f5f79f098f69..29ba65b760346e 100644 --- a/doc/api/util.md +++ b/doc/api/util.md @@ -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 = { diff --git a/test/parallel/test-util-inspect.js b/test/parallel/test-util-inspect.js index cd7d08caf0a0f8..1d37e97897dd34 100644 --- a/test/parallel/test-util-inspect.js +++ b/test/parallel/test-util-inspect.js @@ -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 }' );