TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

SOLVED: How can I sort SwiftData Query results by a bool?

Forums > SwiftUI

Lets say I have a @Model class like this.

@Model
class MyObject {
    var isActive: Bool
    var name: String
}

And I want my query results to show active objects first, in alphabetical order by name, then inactive objects after, also in alphabetical order by name.

I thought that I would be able to use a Query like this...

@Query(sort: [SortDescriptor(\MyObject.isActive), SortDescriptor(\MyObject.name)]) var objects: [MyObject]

But that gives me an error saying "No exact matches in call to initializer". So, I guess it doesn't know how to sort by Bool even though that seems like it should be easy to do.

So, do I have to create 2 separate queries? One that filters out all of the inactive results, and one that filters out all of the active results,

@Query(
    filter: #Predicate<MyObject> { object in
        object.isActive
    },

    sort: [SortDescriptor(\MyObject.name)]

) var activeObjects: [MyObject]

@Query(
    filter: #Predicate<MyObject> { object in
        !object.isActive
    },

    sort: [SortDescriptor(\MyObject.name)]

) var inactiveObjects: [MyObject]

and then just add them both to a List using separate ForEach loops...

var body: some View {
    List {
        ForEach(activeObjects) { object in
            NavigationLink(value: object) {
                Text("\(object.name)")
            }
        }

        ForEach(inactiveObjects) { object in
            NavigationLink(value: object) {
                Text("\(object.name)")
            }
        }
    }
}

Or is there some easier way to do it with just one Query that I'm missing?

3      

hi,

consider making the type Bool conform to the Comparable protocol. i have used this in the past for sorting booleans (although i have not tested with SwiftData yet).

extension Bool: Comparable {
    public static func <(lhs: Self, rhs: Self) -> Bool {
        // the only true inequality is false < true
        !lhs && rhs
    }
}

hope that helps,

DMG

6      

Yes, that does get rid of the error, so I suppose that could be one way of doing it. Thanks.

3      

Hacking with Swift is sponsored by Blaze.

SPONSORED Still waiting on your CI build? Speed it up ~3x with Blaze - change one line, pay less, keep your existing GitHub workflows. First 25 HWS readers to use code HACKING at checkout get 50% off the first year. Try it now for free!

Reserve your spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.