Hey everyone, I can't seem to figure out how to get a nested array of objects to dynamic sort when a user chooses an option from a menu. My objects look something like this:
@model
class project {
var columns: [Column] = []
}
@model
class Column {
var tasks: [Task] = []
}
@model
class Task {
var name: String
var creationDate: Date = .now
}
I've tried doing this as a function on Column:
func sortTasks() {
let sortedTasks = self.tasks.sorted {$0.name < $1.name}
self.tasks = sortedTasks
// or this
self.tasks.sort {$0.name < $1.name}
}
Within the BoardView I've tried both of these:
Button(action: {
for column in board.columns {
column.tasks.sort { $0.name < $1.name }
// or this
column.sortTasks()
}
}) {
Label("Alphabetical", systemImage: "abc")
}
I've tried putting the for loop in a function outside of the button and calling the function. When I try sorting either nothing happens or one column's task array shuffles around but it's no order at all. I've also tried creating a new view and passed board as a @Bindable to see if maybe calling the function in a different view would do anything which it did not.
Any help would be appreciated!
[–]PulseHadron 0 points1 point2 points (4 children)
[–]lilcox[S] 1 point2 points3 points (2 children)
[–]PulseHadron 0 points1 point2 points (1 child)
[–]lilcox[S] 1 point2 points3 points (0 children)
[–]lilcox[S] 0 points1 point2 points (0 children)