Skip to content

Instantly share code, notes, and snippets.

@edgarberm
Created February 20, 2015 20:20
Show Gist options
  • Save edgarberm/41cb5d23fb323c4f8439 to your computer and use it in GitHub Desktop.
Save edgarberm/41cb5d23fb323c4f8439 to your computer and use it in GitHub Desktop.
Extend JavaScript Array sortOn
Array.prototype.sortOn = function() {
var b = this.slice();
if (!arguments.length) return b.sort();
var e = Array.prototype.slice.call(arguments);
return b.sort(function(b, g) {
for (var h = e.slice(), c = h.shift(); b[c] == g[c] && h.length;) c = h.shift();
return b[c] == g[c] ? 0 : b[c] > g[c] ? 1 : -1
})
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment