I think this has to be a simple fix, but I can't find it.
I want to sort information by date and by type. But the type sort isn't working. I move elements from type to type and they stay in the same order. I think this is because the time element of the date is keeping them in the same order because when I move the dates, they resort. But if they're all on the same date, they remain in the same order no matter what type I give them.
Is there a way to write a Sort array and tell it to ignore the time component of the timestamp the way you can omit time when formatting how a date is displayed?
MODEL
@Model
class Meal: Identifiable {
let id = UUID()
var title: String = ""
var type: String = ""
var date: Date = Date()
init(title: String, type: String, date: Date) {
self.title = title
self.type = type
self.date = date
}
}
SORT
@Query(sort: [SortDescriptor(\Meal.date), SortDescriptor(\Meal.type)]) private var meals: [Meal]
DISPLAY
HStack{
Label(meal.title, systemImage: "cup.and.saucer")
Spacer()
HStack{
Text(meal.date.formatted( Date.FormatStyle()
.weekday(.abbreviated)
))
Text(meal.date.formatted( Date.FormatStyle()
.day(.twoDigits)
.month(.abbreviated)
))
}
}