SwiftData @Query with a Custom (non-standard) Sort Criterion

I have a @Query which I would like to sort using a custom criterion. I would like to use an array of SortDescriptors where I can specify a key path and a CUSTOM SortComparator. (I.e., I can write SortComparators which do what I want, but I can't use them in a SortDescriptor because there is no initializer which takes a SortCompartor which is not a String.StandardComparator.)

This is not a question about dynamic sorting, which has well-known solutions. I am trying to perform a sort which cannot be satisfied by any existing SortDescriptor (or array of SortDescriptors).

Any thoughts about how to resolve this situation?

I don't think this is currently possible. init(_:comparing:) requires that the compared type inherits from NSObject, and yet, making a SwiftData model a subclass of NSObject triggers an error in PersistentModel.

I’d suggest that you file a feedback report – If you do so, please share your report ID here. Thanks.

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

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.

SwiftData &#64;Query with a Custom (non-standard) Sort Criterion
 
 
Q