//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);
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter