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.
Thanks for all great answers. For information, I discovered that a recursive approach is also an alternative solution
function sortByThenBy(items, keys) {
return items.sort(function(it1, it2){return compare(it1, it2, keys);});
}
function compare(it1, it2, keys, index) {
index = index || 0;
var currentKey = keys[index];
return it1[currentKey] < it2[currentKey] ? 1 : (it1[currentKey] > it2[currentKey] ? -1 : compare(it1, it2, keys, index + 1));
}