From 25c91b398a78d2b8649244ca3bc69841e1ec70a9 Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Thu, 8 Aug 2013 22:23:17 -0700 Subject: [PATCH] Fix InDesign bug with `_.sortBy`. Former-commit-id: 3ed2c5fcb02885a9b3563cf9081f6e2af8826c6d --- lodash.js | 6 +++++- test/test.js | 14 ++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/lodash.js b/lodash.js index cefc1170e..fa4395c2b 100644 --- a/lodash.js +++ b/lodash.js @@ -256,7 +256,11 @@ return -1; } } - return ai < bi ? -1 : 1; + // The JS engine embedded in Adobe applications like InDesign has a buggy + // `Array#sort` implementation that causes it, under certain circumstances, + // to return the same value for `a` and `b`. + // See https://github.com/jashkenas/underscore/pull/1247 + return ai < bi ? -1 : (ai > bi ? 1 : 0); } /** diff --git a/test/test.js b/test/test.js index 810e65c20..0022e7d10 100644 --- a/test/test.js +++ b/test/test.js @@ -3151,6 +3151,20 @@ QUnit.module('lodash.sortBy'); (function() { + test('should sort in ascending order', function() { + var actual = _.pluck(_.sortBy([ + { 'num': 991 }, + { 'num': 212 }, + { 'num': 11 }, + { 'num': 16 }, + { 'num': 74 }, + { 'num': 0 }, + { 'num': 1515 } + ], 'num'), 'num'); + + deepEqual(actual, [0, 11, 16, 74, 212, 991, 1515]); + }); + test('should perform a stable sort (test in IE > 8, Opera, and V8)', function() { function Pair(x, y) { this.x = x;