Skip to content

Commit

Permalink
SimpleQuery: only call array.sort if we have some sort criteria
Browse files Browse the repository at this point in the history
Fixes #159
  • Loading branch information
msssk committed Apr 9, 2020
1 parent 1a75538 commit e59daa3
Showing 1 changed file with 38 additions and 29 deletions.
67 changes: 38 additions & 29 deletions SimpleQuery.js
Expand Up @@ -181,42 +181,51 @@ define([
};
},

_createSortQuerier: function (sorted) {
_createSortQuerier: function (sortOptions) {
var queryAccessors = this.queryAccessors;

return function (data) {
data = data.slice();
data.sort(typeof sorted == 'function' ? sorted : function (a, b) {
for (var i = 0; i < sorted.length; i++) {
var comparison;
var sorter = sorted[i];
if (typeof sorter == 'function') {
comparison = sorter(a, b);
} else {
var getProperty = sorter.get || (sorter.get = makeGetter(sorter.property, queryAccessors));
var descending = sorter.descending;
var aValue = getProperty(a);
var bValue = getProperty(b);

aValue != null && (aValue = aValue.valueOf());
bValue != null && (bValue = bValue.valueOf());
if (aValue === bValue) {
comparison = 0;

if (typeof sortOptions === 'function') {
data.sort(sortOptions);
}
else if (sortOptions && sortOptions.length) {
data.sort(function (a, b) {
for (var i = 0; i < sortOptions.length; i++) {
var comparison;
var sorter = sortOptions[i];
if (typeof sorter == 'function') {
comparison = sorter(a, b);
} else {
var getProperty = sorter.get || (sorter.get = makeGetter(sorter.property, queryAccessors));
var descending = sorter.descending;
var aValue = getProperty(a);
var bValue = getProperty(b);

aValue != null && (aValue = aValue.valueOf());
bValue != null && (bValue = bValue.valueOf());
if (aValue === bValue) {
comparison = 0;
}
else {
// Prioritize undefined > null > defined
var isALessThanB = typeof bValue === 'undefined' ||
bValue === null && typeof aValue !== 'undefined' ||
aValue != null && aValue < bValue;
comparison = Boolean(descending) === isALessThanB ? 1 : -1;
}
}
else {
// Prioritize undefined > null > defined
var isALessThanB = typeof bValue === 'undefined' ||
bValue === null && typeof aValue !== 'undefined' ||
aValue != null && aValue < bValue;
comparison = Boolean(descending) === isALessThanB ? 1 : -1;

if (comparison !== 0) {
return comparison;
}
}

if (comparison !== 0) {
return comparison;
}
}
return 0;
});
return 0;
});
}

return data;
};
}
Expand Down

0 comments on commit e59daa3

Please sign in to comment.