diff --git a/FuelBoard/ContentView.swift b/FuelBoard/ContentView.swift index 178f01b..d4bd163 100644 --- a/FuelBoard/ContentView.swift +++ b/FuelBoard/ContentView.swift @@ -19,15 +19,13 @@ struct ContentView: View { stations .filter { $0.prices[selectedFuel] != nil } .sorted { lhs, rhs in - // Prefer distance when we have a location, otherwise price. - guard let location else { - return lhs.prices[selectedFuel]! < rhs.prices[selectedFuel]! - } - let lScore = lhs.distanceKM(to: location.lat, lng2: location.lng) * 0.4 + - lhs.prices[selectedFuel]! * 0.001 - let rScore = rhs.distanceKM(to: location.lat, lng2: location.lng) * 0.4 + - rhs.prices[selectedFuel]! * 0.001 - return lScore < rScore + // Cheapest first; distance only breaks ties. + let lPrice = lhs.prices[selectedFuel]! + let rPrice = rhs.prices[selectedFuel]! + if lPrice != rPrice { return lPrice < rPrice } + guard let location else { return false } + return lhs.distanceKM(to: location.lat, lng2: location.lng) < + rhs.distanceKM(to: location.lat, lng2: location.lng) } } diff --git a/FuelBoardWidgets/FuelPriceWidget.swift b/FuelBoardWidgets/FuelPriceWidget.swift index 67fd324..8f85b94 100644 --- a/FuelBoardWidgets/FuelPriceWidget.swift +++ b/FuelBoardWidgets/FuelPriceWidget.swift @@ -91,9 +91,12 @@ struct FuelPriceTimelineProvider: TimelineProvider { let sorted: [FuelStation] if let location { sorted = filtered.sorted { lhs, rhs in - let lScore = lhs.distanceKM(to: location.lat, lng2: location.lng) * 0.4 + lhs.prices[fuel]! * 0.001 - let rScore = rhs.distanceKM(to: location.lat, lng2: location.lng) * 0.4 + rhs.prices[fuel]! * 0.001 - return lScore < rScore + // Cheapest first; distance only breaks ties. + let lPrice = lhs.prices[fuel]! + let rPrice = rhs.prices[fuel]! + if lPrice != rPrice { return lPrice < rPrice } + return lhs.distanceKM(to: location.lat, lng2: location.lng) < + rhs.distanceKM(to: location.lat, lng2: location.lng) } } else { sorted = filtered.sorted { $0.prices[fuel]! < $1.prices[fuel]! }