From 5be86856a934408c0351740fd9aed6e06572e1ce Mon Sep 17 00:00:00 2001 From: krugerk <4656811+krugerk@users.noreply.github.com> Date: Wed, 23 Oct 2024 19:15:30 +0200 Subject: [PATCH] fancy swift filter and sorted(using:) (#498) Using `.filter` for increased legibility. And using `.sorted(using:)` to as a cleaner way to achieve the same result as the manual closure-based sorting, since the manual closure-based sort was only checking one keypath item anyway. --- BeeKit/GoalExtensions.swift | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/BeeKit/GoalExtensions.swift b/BeeKit/GoalExtensions.swift index 2a9e13a4c..d9d511874 100644 --- a/BeeKit/GoalExtensions.swift +++ b/BeeKit/GoalExtensions.swift @@ -80,13 +80,10 @@ extension Goal { /// A hint for the value the user is likely to enter, based on past data points public var suggestedNextValue: NSNumber? { - let recentData = self.recentData - for dataPoint in recentData.sorted(by: { $0.updatedAt > $1.updatedAt }) { - if dataPoint.isMeta() { - continue - } - return dataPoint.value - } - return nil + let candidateDatapoints = self.recentData + .filter { !$0.isMeta() } + .sorted(using: [SortDescriptor(\.updatedAt, order: .reverse)]) + + return candidateDatapoints.first?.value } }