Hey all, I'm having trouble figuring out how to sort a nested array within SwiftData.
here is how i have my model's designed:
@model
class project {
var columns: [Column] = []
}
@model
class Column {
var tasks: [Task] = []
func sortTasks() {
self.tasks.sort {$0.name < $1.name}
}
}
@model
class Task {
var name: String
var creationDate: Date = .now
}
I'm doing an @Query and forEach in ContentView but afterwards
@Query(sort: \Board.creationDate) var boards: [Board]
ForEach(boards) { board in
NavigationLink(value: board) {
SideBarItemView(icon: board.icon, name: board.name)
.tag(board)
}
}
.navigationDestination(for: Board.self) { board in
BoardView(board: board, columnVisibility: $columnVisibility)
}
My BoardView code:
@Bindable var board: Board
ForEach(board.columns.sorted(by: {$0.creationDate < $1.creationDate})) { column in
ColumnView(board: board, column: column)
}
Button(action: {
column.sortTasks()
}) {
Label("Alphabetical", systemImage: "abc")
}
Any help would be greatly appreciated.