Skip to content

Commit 79abeda

Browse files
committed
Fix sorting of nulls and numbers, fixes #78
1 parent faebeb6 commit 79abeda

File tree

2 files changed

+45
-3
lines changed

2 files changed

+45
-3
lines changed

SimpleQuery.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,9 @@ define([
106106
// returns: Function?
107107

108108
return comparators[type] || this.inherited(arguments);
109-
},
110-
109+
}
111110
/* jshint ignore:start */
111+
,
112112
_createSortQuerier: function (sorted) {
113113
return function (data) {
114114
data = data.slice();
@@ -128,7 +128,7 @@ define([
128128

129129
comparison = aValue === bValue
130130
? 0
131-
: (!!descending === (aValue === null || aValue > bValue) ? -1 : 1);
131+
: (!!descending === (aValue === null || aValue > bValue && bValue !== null) ? -1 : 1);
132132
}
133133

134134
if (comparison !== 0) {

tests/Memory.js

+42
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,48 @@ define([
435435
assert.deepEqual(testStore.actualData, expectedData);
436436
},
437437

438+
'sorting with null and numbers': function () {
439+
var store = new Memory({
440+
data: [
441+
{id: 1, value: 1},
442+
{id: 2, value: 2},
443+
{id: 3, value: null},
444+
{id: 4, value: -1}
445+
]
446+
});
447+
var results = store.sort('value').fetchSync();
448+
assert.equal(results[0].id, 4);
449+
assert.equal(results[1].id, 1);
450+
assert.equal(results[2].id, 2);
451+
assert.equal(results[3].id, 3);
452+
var results = store.sort('value', true).fetchSync();
453+
assert.equal(results[0].id, 3);
454+
assert.equal(results[1].id, 2);
455+
assert.equal(results[2].id, 1);
456+
assert.equal(results[3].id, 4);
457+
},
458+
459+
'sorting with null and strings': function () {
460+
var store = new Memory({
461+
data: [
462+
{id: 1, value: 'a'},
463+
{id: 2, value: 'b'},
464+
{id: 3, value: null},
465+
{id: 4, value: ''}
466+
]
467+
});
468+
var results = store.sort('value').fetchSync();
469+
assert.equal(results[0].id, 4);
470+
assert.equal(results[1].id, 1);
471+
assert.equal(results[2].id, 2);
472+
assert.equal(results[3].id, 3);
473+
var results = store.sort('value', true).fetchSync();
474+
assert.equal(results[0].id, 3);
475+
assert.equal(results[1].id, 2);
476+
assert.equal(results[2].id, 1);
477+
assert.equal(results[3].id, 4);
478+
},
479+
438480
nestedSuite: sorting('dstore Memory sorting', function before(data) {
439481
return function before() {
440482
store = new Memory({data: data});

0 commit comments

Comments
 (0)