I have an array of posts which I currently group by day. I use the below functions to do so;
private func splitDay(from date: Date) -> Date {
let calendar = Calendar.current
let components = calendar.dateComponents([.year, .month, .day], from: date)
return calendar.date(from: components)!
}
private func sectionPosts(posts: [Post]) {
let groups = Dictionary(grouping: posts) { (posts) in
return splitDay(from: Date(timeIntervalSince1970: posts.createdOn))
}
self.sections = groups.map { (date, posts) in
return PostSection(date: date, posts: posts)
}
}
However, i'd like to implement a custom grouping which would be the following;
- Today
- Yesterday
- This Week
- This Month
- Older
How would I build this into my grouping function? My section struct is like such;
struct PostSection {
var date: Date
var posts: [Post]
}