Skip to content

Instantly share code, notes, and snippets.

@manix
Last active March 1, 2024 23:51
Show Gist options
  • Save manix/a2160e7084c5c7fef5431af359fb342e to your computer and use it in GitHub Desktop.
Save manix/a2160e7084c5c7fef5431af359fb342e to your computer and use it in GitHub Desktop.
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