I have two arrays. One array is the array of items needing to be sorted. Another array is the keys (properties of that object) to sort by. I am wanting to have a function that sorts the array by each key given.
I have tried to loop over the keys array and pop each key off of the array and then sort but adding that key to the ternary I am using to sort the array has been giving me issues.
export function sortOrdersByKeys<T>(ordersArr: T[], sortByKeys: string[]): T[]
{
if (sortByKeys.length === 0) {
return ordersArr;
} else {
const lastItem = sortByKeys.pop();
return sortWithKey(ordersArr, lastItem);
}
}
function sortWithKey(arr, key) {
key = key[0];
for (let i = 0; i < key.length(); i++) {
}
return arr.sort((a, b) => (a.key > b.key) ? 1 : -1);
}