Commit 94fc1a5f authored by Romain Courteaud's avatar Romain Courteaud

No need to duplicate the algorithm.

parent 11204f91
...@@ -52,31 +52,15 @@ function metadataValueToStringArray(value) { ...@@ -52,31 +52,15 @@ function metadataValueToStringArray(value) {
* @return {Function} The sort function * @return {Function} The sort function
*/ */
function sortFunction(key, way) { function sortFunction(key, way) {
var result;
if (way === 'descending') { if (way === 'descending') {
return function (a, b) { result = 1;
// this comparison is 5 times faster than json comparison } else if (way === 'ascending') {
var i, l; result = -1;
a = metadataValueToStringArray(a[key]) || []; } else {
b = metadataValueToStringArray(b[key]) || []; throw new TypeError("Query.sortFunction(): " +
l = a.length > b.length ? a.length : b.length; "Argument 2 must be 'ascending' or 'descending'");
for (i = 0; i < l; i += 1) {
if (a[i] === undefined) {
return 1;
}
if (b[i] === undefined) {
return -1;
}
if (a[i] > b[i]) {
return -1;
}
if (a[i] < b[i]) {
return 1;
}
}
return 0;
};
} }
if (way === 'ascending') {
return function (a, b) { return function (a, b) {
// this comparison is 5 times faster than json comparison // this comparison is 5 times faster than json comparison
var i, l; var i, l;
...@@ -85,23 +69,20 @@ function sortFunction(key, way) { ...@@ -85,23 +69,20 @@ function sortFunction(key, way) {
l = a.length > b.length ? a.length : b.length; l = a.length > b.length ? a.length : b.length;
for (i = 0; i < l; i += 1) { for (i = 0; i < l; i += 1) {
if (a[i] === undefined) { if (a[i] === undefined) {
return -1; return result;
} }
if (b[i] === undefined) { if (b[i] === undefined) {
return 1; return -result;
} }
if (a[i] > b[i]) { if (a[i] > b[i]) {
return 1; return -result;
} }
if (a[i] < b[i]) { if (a[i] < b[i]) {
return -1; return result;
} }
} }
return 0; return 0;
}; };
}
throw new TypeError("Query.sortFunction(): " +
"Argument 2 must be 'ascending' or 'descending'");
} }
/** /**
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment