diff --git a/lib/cursor.js b/lib/cursor.js index 8fad40c..bd4df63 100644 --- a/lib/cursor.js +++ b/lib/cursor.js @@ -84,16 +84,17 @@ Cursor.prototype._exec = function(callback) { if (this._sort) { keys = Object.keys(this._sort); - // Going backwards so that the first sort is the last that gets applied + // Sorting var criteria = []; for (i = 0; i < keys.length; i++) { key = keys[i]; criteria.push({ key: key, direction: self._sort[key] }); } res.sort(function(a, b) { - for (var i = 0; i < criteria.length; i++) { - var criterion = criteria[i]; - var compare = criterion.direction * model.compareThings(model.getDotValue(a, criterion.key), model.getDotValue(b, criterion.key)); + var criterion, compare, i; + for (i = 0; i < criteria.length; i++) { + criterion = criteria[i]; + compare = criterion.direction * model.compareThings(model.getDotValue(a, criterion.key), model.getDotValue(b, criterion.key)); if (compare !== 0) { return compare; } diff --git a/test/cursor.test.js b/test/cursor.test.js index d464cb0..fcf026d 100644 --- a/test/cursor.test.js +++ b/test/cursor.test.js @@ -606,16 +606,19 @@ describe('Cursor', function () { ], done); }); it('Similar data, multiple consecutive sorts', function(done) { + var i, j, id + , companies = [ 'acme', 'milkman', 'zoinks' ] + , entities = [] + ; + async.waterfall([ function (cb) { d.remove({}, { multi: true }, function (err) { if (err) { return cb(err); } - - var entities = []; - var companies = [ 'acme', 'milkman', 'zoinks' ]; - var id = 1; - for (var i = 0; i < companies.length; i++) { - for (var j = 5; j <= 100; j += 5) { + + id = 1; + for (i = 0; i < companies.length; i++) { + for (j = 5; j <= 100; j += 5) { entities.push({ company: companies[i], cost: j, @@ -632,7 +635,6 @@ describe('Cursor', function () { }, function(err) { return cb(); }); - }); } , function (cb) {