My goal is to simplify the process of sorting both number and string values. The first step involves checking if the parameter I've passed (which belongs to the DeliveryDetailsColumns constants) matches another parameter from a different type (ElectronicDeliveryType).
export const DeliveryDetailsColumns = {
Title: "title",
ExpectedDelivery: "expectedDelivery",
Price: "price",
Quantity: "quantity",
};
export interface ElectronicDeliveryType {
title?: string;
quantity?: number;
price?: number;
expectedDelivery?: string;
}
I'm currently working with an array of objects named filteredValues in pinia, which are of type ElectronicDeliveryType. My aim is to sort the selected column, but my current method is not very generic. I resort to a switch case that loops through all the options in DeliveryDetailsColumns. I am looking for a more generic solution that involves checking the names of each property in ElectronicDeliveryType against those in DeliveryDetailsColumns and verifying their types. What would be the best approach here?
sortBySelectedColumnOrder(columnName: string, sortOrder: string) {
if (sortOrder === "ascending") {
switch (columnName) {
case DeliveryDetailsColumns.Title:
this.filteredValues.sort((a, b) =>
a!.title!.toLowerCase() > b!.title!.toLowerCase() ? 1 : -1
);
break;
case DeliveryDetailsColumns.Price:
this.filteredValues.sort((a, b) => a!.price! - b!.price!);
break;
[...]