Is there a way to get a SwiftUI Table to be able to sort by a column of optionals?
You can extend Optional for your specific case. For example, if you are trying to sort by an optional Int:
extension Optional where Wrapped == Int {
var sortOrder: Int {
switch self {
case let .some(wrapped): wrapped
case .none: Int.max
}
}
}
Then your TableColumn would look like this:
TableColumn("Count", value: \.count.sortOrder) {
if let count = $0.count {
Text(String(count))
}
}
If count is nil, it will be sorted as if its value is Int.max and the column will be blank for that row.
Topic:
UI Frameworks
SubTopic:
SwiftUI
Tags: