How to write a generic sorting function in the style orderBy thenBy that sort an array by a list of properties provided as an array.
I think you can do as follows;
function sortBy(a,p){
return a.sort(function(a,b){
var sp = p.find(k => a[k] - b[k]); // find the property to compare;
return a[sp] - b[sp];
});
}
var items = [{ name: "AA", prop1 : 12, prop2: 13, prop3: 5, prop4: 26 },
{ name: "AA", prop1 : 12, prop2: 13, prop3: 6, prop4: 23 },
{ name: "AA", prop1 : 12, prop2: 14, prop3: 5, prop4: 23 },
{ name: "AA", prop1 : 11, prop2: 13, prop3: 5, prop4: 22 },
{ name: "AA", prop1 : 10, prop2: 13, prop3: 9, prop4: 21 }
];
console.log(sortBy(items,["prop1", "prop3", "prop4"]));
console.log(sortBy(items,["prop1", "prop3"]));
console.log(sortBy(items,["prop4", "prop3"]));