Can ObservedResults sort on more than one keypath?

A regular Results object can be sorted by called the sorted method and passing in an array of sort descriptors.

The ObservedResults property wrapper seems to only allow passing in a single sort descriptor. Is there a way to use multiple sorts while using ObservedResults.

For example, if I have a list of people I may want to sort the list by last name first and then first name. Unless I am missing something obvious, which is definitely possible, I don’t see a way to do with using ObservedResults.

Any help would be much appreciated.


Hi @Nick_Harbin – welcome to the community forum!

I believe that you’re correct that you can only provide a single key-path to the @ObservedResults declaration.

If you want to sort on multiple fields then you can sort the results within the body of your view…

var body: some View {
        let sortedResults = results.sorted(by: [
            SortDescriptor(keyPath: "field1"),
            SortDescriptor(keyPath: "field1")])
        return VStack {
1 Like