From 3620b9ed6049a8b7e1a9a9f0f19589d322a026d7 Mon Sep 17 00:00:00 2001 From: krugerk <4656811+krugerk@users.noreply.github.com> Date: Mon, 30 Dec 2024 01:53:08 +0100 Subject: [PATCH] list does not match website when sorting by pledge (#579) ## Summary When sorting by pledge, goals were sorted grouped by pledge amounts. The sort order within seemed undefined. Also used the opportunity to clean the code somewhat using `sorted(using:)` where multiple comparators may be provided. ## Validation Ran app in simulator. Compared before and after to list when sorting on the website. Fixes #578 --- BeeSwift/Gallery/GalleryViewController.swift | 31 ++++++++++---------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/BeeSwift/Gallery/GalleryViewController.swift b/BeeSwift/Gallery/GalleryViewController.swift index b43645486..454e7fea4 100644 --- a/BeeSwift/Gallery/GalleryViewController.swift +++ b/BeeSwift/Gallery/GalleryViewController.swift @@ -357,22 +357,21 @@ class GalleryViewController: UIViewController, UICollectionViewDelegateFlowLayou } func sortedGoals(_ goals: any Sequence) -> [Goal] { - return goals.sorted(by: { (goal1, goal2) -> Bool in - if let selectedGoalSort = UserDefaults.standard.value(forKey: Constants.selectedGoalSortKey) as? String { - if selectedGoalSort == Constants.nameGoalSortString { - return goal1.slug < goal2.slug - } - else if selectedGoalSort == Constants.recentDataGoalSortString { - return goal1.lastTouch > goal2.lastTouch - } - else if selectedGoalSort == Constants.pledgeGoalSortString { - return goal1.pledge > goal2.pledge - } - } - - // urgencykey is guaranteed to result in goals sorting into the canonical order - return goal1.urgencyKey < goal2.urgencyKey - }) + let selectedGoalSort = UserDefaults.standard.value(forKey: Constants.selectedGoalSortKey) as? String ?? Constants.urgencyGoalSortString + + switch selectedGoalSort { + case Constants.nameGoalSortString: + return goals.sorted(using: [SortDescriptor(\.slug), + SortDescriptor(\.urgencyKey)]) + case Constants.recentDataGoalSortString: + return goals.sorted(using: [SortDescriptor(\.lastTouch, order: .reverse), + SortDescriptor(\.urgencyKey)]) + case Constants.pledgeGoalSortString: + return goals.sorted(using: [SortDescriptor(\.pledge, order: .reverse), + SortDescriptor(\.urgencyKey)]) + default: + return goals.sorted(using: SortDescriptor(\.urgencyKey)) + } } func updateFilteredGoals() {