Skip to content

Instantly share code, notes, and snippets.

@manix manix/sample.js
Last active Apr 27, 2019

Embed
What would you like to do?
Improved "fieldSorter"
function fieldSorterOptimized(fields) {
var dir = [], i, l = fields.length;
fields = fields.map(function(o, i) {
if (o[0] === "-") {
dir[i] = -1;
o = o.substring(1);
} else {
dir[i] = 1;
}
return o;
});
return function (a, b) {
for (i = 0; i < l; i++) {
var o = fields[i];
if (a[o] > b[o]) return dir[i];
if (a[o] < b[o]) return -(dir[i]);
}
return 0;
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.