I've looked at all the solutions that only go 1 level deep, but I want to make a generalized solution, a function to which I can pass what inner properties to compare, something like
var array = [
{grand: {child: {property: 2}}},
{grand: {child: {property: 1}}},
{grand: {child: {property: 3}}},
];
var sorted = array.sortBy('grand.child.property');
Like that, generally passing the 'dot.property.notation'
as a string.
But I can't figure out a way to do that with Array.sort
's comparator function
.
Array.prototype.sortBy = function(predicate){
return this.sort(function(a, b){
// how to get a[.grand.child.property] from the
// parameter string 'grand.child.property'?
});
};