In my sortDynamic
function, I am attempting to dynamically sort data like this:
const sortDynamic = (key: string, order: string) => {
const sortOrder = order === 'asc' ? 1 : -1;
return (a: ProductMapData, b: ProductMapData) => {
const A = typeof a[key] === 'string' ? a[key].toUpperCase() : a[key];
const B = typeof b[key] === 'string' ? b[key].toUpperCase() : b[key];
if (A < B) {
return sortOrder * -1;
} else if (A > B) {
return sortOrder * 1;
} else {
return 0;
}
};
};
The ProductMapData
interface looks like this:
interface ProductMapData {
advanceRevenue: number;
advanceTickets: number;
changeInPeriodRevenue: number;
changeInPeriodTickets: number;
currency: string;
entityRef: string;
eopAdvanceRevenue: number;
eopAdvanceTickets: number;
hallLabel: string;
occurredAt: string | undefined;
playedOffRevenue: number;
playedOffTickets: number;
relatedEventName: string;
thumbnail: string;
timeBegins: string;
timedBeginsBegins: string;
soldTickets: number;
soldRevenue: number;
}
I am calling the function here:
productMapData.sort(sortDynamic('soldTickets', 'asc'));
However, I am encountering an error message that says
Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'ProductMapData'. No index signature with a parameter of type 'string' was found on type 'ProductMapData'.ts(7053)
on a[key]
and b[key]
. I'm unsure of where the issue lies. Any assistance would be greatly appreciated.