Reputation: 13
I have this array:
let arraRisultati = DataManager.shared.storage.filter { $0.Data < NSDate() as Date }
Can I order its elements in chronological order with the "sort" function?
Upvotes: 0
Views: 47
Reputation: 385590
First, if you want to get only elements whose date is prior to now, it would be better to pick up the current date just once, and you can do it directly as a Date
:
let now = Date()
let arraRisultati = DataManager.shared.storage.filter { $0.date < now }
Since Date
is Comparable
, you can sort on it like this:
let sortedResults = arraRisultati.sorted(by: { $0.date < $1.date })
You can do it in one expression if you prefer:
let now = Date()
let arraRisultati = DataManager.shared.storage.filter({ $0.date < now }).sorted(by: { $0.date < $1.date })
Upvotes: 1