Make Devices table columns sortable#20
Merged
Merged
Conversation
Bind a sortOrder state to the Table and add value: KeyPaths on each TableColumn so headers expose ascending/descending sort indicators. Optional String attributes get non-optional sort keys via a private extension that falls back to "" for nil values.
There was a problem hiding this comment.
Pull request overview
Adds user-driven sorting to the Devices table by wiring Table’s sortOrder and providing sortable value: key paths (including non-optional sort keys for optional attributes), with a default sort on Serial Number.
Changes:
- Bind
sortOrderto theTableand sort the backing collection usingdevices.sorted(using: sortOrder). - Update each
TableColumnto supply avalue:key path so headers can drive sorting. - Introduce private
DeviceAttributessort-key helpers that map optionals to non-optionals for sorting.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
63
to
65
| TableColumn("Storage", value: \.sortDeviceCapacity) { (d: DeviceAttributes) in | ||
| Text(d.deviceCapacity ?? "") | ||
| } |
Comment on lines
+22
to
+24
| private var sortedDevices: [DeviceAttributes] { | ||
| devices.sorted(using: sortOrder) | ||
| } |
Comment on lines
+10
to
+12
| var sortOrderNumber: String { orderNumber ?? "" } | ||
| var sortOrderDateTime: String { orderDateTime ?? "" } | ||
| var sortUpdatedDateTime: String { updatedDateTime ?? "" } |
…ed sorted devices - Storage column sorts by numeric rank (64GB < 256GB < 1TB) instead of string. - Order Date / Updated columns sort on parsed ISO8601 Date so fractional- seconds variants order chronologically. - Cache sorted devices in @State and refresh on devices/sortOrder changes so selection updates don't re-sort the whole table on every body pass.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
sortOrderstate to the DevicesTableso column headers expose ascending/descending sort indicators.TableColumnnow usesvalue: \.<keyPath>; optionalString?attributes get non-optional sort keys via a privateDeviceAttributesextension that falls back to""for nil.Test plan