import { HotelSortOption } from "../types/hotel" import type { DestinationCityListItem } from "../types/destinationCityPage" const CITY_SORTING_STRATEGIES: Partial< Record< HotelSortOption, (a: DestinationCityListItem, b: DestinationCityListItem) => number > > = { [HotelSortOption.Name]: function (a, b) { return a.cityName.localeCompare(b.cityName) }, [HotelSortOption.Recommended]: function (a, b) { if (a.sort_order === null && b.sort_order === null) { return a.cityName.localeCompare(b.cityName) } if (a.sort_order === null) { return 1 } if (b.sort_order === null) { return -1 } return b.sort_order - a.sort_order }, } export function getSortedCities( cities: DestinationCityListItem[], sortOption: HotelSortOption ) { const sortFn = CITY_SORTING_STRATEGIES[sortOption] return sortFn ? cities.sort(sortFn) : cities }