[Vuejs]-Sort by specific object property value (number, string) optimization

2👍

Make a map of delivery details to their respective comparator function:

const comparators = new Map<
    keyof typeof DeliveryDetailsColumns,
    (a: ElectronicDeliveryType, b: ElectronicDeliveryType) => number
>([
    [DeliveryDetailsColumns.Title, (a, b) => a!.title!.toLowerCase() > b!.title!.toLowerCase() ? 1 : -1],   
    [DeliveryDetailsColumns.Price, (a, b) => a!.price! - b!.price!)],
]);

Then you can just do a lookup on the map:

this.filteredValues.sort(comparators.get(columnName)!);

But make sure to check if the column exists first:

if (!comparators.has(columnName)) { ... }
👤kelsny

Leave a comment