I’m trying to sort an array of objects. I’d prefer not to write a custom sort method for each attribute.
Is there anyway I could extend the built-in array.sort() method to accept an extra parameter, describing the attribute to sort on? E.g.,
array.sort(function(a, b, attr) { return a.attr - b.attr; }, 'name');
Write a function generator that accepts a property name:
You can also save the sorters for later use, directly, or as parameters:
(Updated for ES6, and so it actually works with different types.)