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

No need to duplicate the algorithm.

parent 11204f91
...@@ -52,56 +52,37 @@ function metadataValueToStringArray(value) { ...@@ -52,56 +52,37 @@ 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 function (a, b) {
return 1; // this comparison is 5 times faster than json comparison
} var i, l;
if (b[i] === undefined) { a = metadataValueToStringArray(a[key]) || [];
return -1; b = metadataValueToStringArray(b[key]) || [];
} l = a.length > b.length ? a.length : b.length;
if (a[i] > b[i]) { for (i = 0; i < l; i += 1) {
return -1; if (a[i] === undefined) {
} return result;
if (a[i] < b[i]) {
return 1;
}
} }
return 0; if (b[i] === undefined) {
}; return -result;
}
if (way === 'ascending') {
return function (a, b) {
// this comparison is 5 times faster than json comparison
var i, l;
a = metadataValueToStringArray(a[key]) || [];
b = metadataValueToStringArray(b[key]) || [];
l = a.length > b.length ? a.length : b.length;
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 (a[i] > b[i]) {
}; return -result;
} }
throw new TypeError("Query.sortFunction(): " + if (a[i] < b[i]) {
"Argument 2 must be 'ascending' or 'descending'"); return result;
}
}
return 0;
};
} }
/** /**
......
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