sorting via switch statement

PHOTO EMBED

Thu Jan 30 2025 23:34:43 GMT+0000 (Coordinated Universal Time)

Saved by @davidmchale #sort #switch

    
//taken from a select option field
selectSortValue = e.target.value; // or whatever matches our swtich condition


function onHandleSort(array, selectOpion) {
        switch (selectOpion) {
            
            case "alphabetical":
             // using slice as to not modify the original array (made a mistake with this!)
              return locations.slice().sort((a, b) => a.name.localeCompare(b.name));

            case "cost":
                return locations.slice().sort((a, b) => a.minimumFee - b.minimumFee);

            case "closest":
                console.log("closest");
                break;

            default:
                return locations; // In case no valid option is selected
        }
    }

   filteredItems = onHandleSort(filteredItems, selectSortValue);
content_copyCOPY