Last active
March 1, 2024 23:51
-
-
Save manix/a2160e7084c5c7fef5431af359fb342e to your computer and use it in GitHub Desktop.
Improved "fieldSorter"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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