From 0ad46f8dcb12ffb65c66795f5632aefa3c80c3e2 Mon Sep 17 00:00:00 2001 From: Jan Stola Date: Mon, 13 Feb 2017 11:32:27 +0100 Subject: [PATCH] Fix of the compare function The previous version of the compare function was not consistent (it did not follow the requirements on the compare function), see https://tc39.github.io/ecma262/#sec-array.prototype.sort for details. You can see that the compare function is not correct by using it to sort larger arrays. For example, if you sort the letters of a well known sentence: `"the quick brown fox jumps over the lazy dog".split('').sort((a,b) => a>b).join('')` returns `"uge d ac be e f otjhmpsnooqrothrkliuvwxyz"` on Node.js 6.7.0. While `"the quick brown fox jumps over the lazy dog".split('').sort((a,b) => a > b ? 1 : (a < b ? -1 : 0)).join('')` returns the correct result `' abcdeeefghhijklmnoooopqrrsttuuvwxyz'`. --- lib/array.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/array.js b/lib/array.js index eb6f71f8..ac756c65 100644 --- a/lib/array.js +++ b/lib/array.js @@ -601,7 +601,7 @@ helpers.withSort = function(array, prop, options) { array.sort(function(a, b) { a = utils.get(a, prop); b = utils.get(b, prop); - return a > b; + return a > b ? 1 : (a < b ? -1 : 0); }); if (utils.get(options, 'hash.reverse')) {