I submitted the following Feedback # FB22949727:
I have need for sorting capabilities in @Query which cannot be met using the available SortDescriptors. (Example below)
Toward that end, I used the SortComparator protocol to create conforming comparators, but could not use them in SortDescriptor because the comparator parameter in all SortDescriptor initializers are type String.StringComparator, not SortComparator. I.e., what I am looking for is a SortDescriptor initializer with a signature something like
init(_ keyPath: any KeyPath<.> & Sendable, comparator: SortComparator, order: SortOrder)
which is not available.
Side Note: Actually, making SortDescriptor more flexible and powerful is generally a good idea because it is also applicable to sort(using:) and similar Foundation functions.
My Actual Example:
I have a SwiftData Model which contains a Date field and a Category field (implemented as an enumeration). A @Query over objects of this Model should result in an array of these objects. The sort of the query should have a primary and secondary SortDescriptor. The primary SortDescriptor sorts by date ignoring time. (I.e., objects with a date on the same day are considered equal — although the time components are relevant in other contexts) The secondary SortDescriptor sorts by category, but in an order that I define which is neither based on the category rawValues string order nor the order in which the enumeration cases are defined.
I would like the sort to take place in the processing of the Query, because then I don’t need to worry about handling all cases where a change in an object should involve a re-sort.