I have an array of UNNotificationRequest
. I want to sort them by nextTriggerDate
.
As far as I understand it, I would sort the array using array.sorted(by:predicate)
let sortedNotifications = notificationRequests.sorted(by:
{ $0.trigger.nextTriggerDate?.compare($1.trigger.nextTriggerDate!) == .orderedAscending })
However, the problem is .trigger
doesn't have a nextTriggerDate
property.
In order to obtain nextTriggerDate
, I have to extract the trigger and cast it into UNCalendarNotificationTrigger
. Which as far as I know, can't be done in a predicate.
Any thoughts?
You can create Tuple
With UNNotificationRequest and nextTriggerDate (UNNotificationRequest,nextTriggerDate)
// get request with date Tuple --> example : (value0,value1)
let requestWithDateTuple = notificationRequests.map({ (req) -> (UNNotificationRequest,Date?)? in
guard let trigger = req.trigger as? UNCalendarNotificationTrigger else {
return nil
}
return (req,trigger.nextTriggerDate())
}).compactMap({$0})
// you will get Tuple (request,Date) ,sort them by date
let sortedTuple = requestWithDateTuple.sorted(by: { $0.1?.compare($1.1!) == .orderedAscending })
// sorted request only
let requestSorted = sortedTuple.map({$0.0})