WWDC22 SALE: Save 50% on all my Swift books and bundles! >>

CoreData Sort Descriptor Not Working

Forums > SwiftUI

Hi I am using CoreData to populate a list of products. I want the list to be sorted by "make" + then "model". Seems simple but for some reason the list does not get sorted. Any Ideas What The Issue Is??

My Fetch Request Is... @FetchRequest(sortDescriptors: [ SortDescriptor(\.make), SortDescriptor(\.model) ]) var products: FetchedResults<Product>

My List Is...

`List { ForEach(products, id: .self) { product in

                            NavigationLink(destination: ProductDetailView(product: product)) {
                                HStack {
                                    Text(product.make ?? "Unknown")
                                    Text(product.model ?? "Unknown")
                                    Text(String(product.tileWidth))
                                }
                            }// END OF NAV LINK
                        }// END OF FOR EACH
                        .onDelete(perform: deleteProducts)
                    }// END OF LIST
                    .frame(height: geometry.size.height * 0.62)
                    .listStyle(InsetGroupedListStyle())`

   

Could you give an example what do you get and what do you expect? Are the data types in make and model correct?

   

Hi make + model are both Strings.

I would expect to get... Ford Escort Ford Sierra Nissan Micra VW Golf

Instead I just get a random order.

   

Hacking with Swift is sponsored by Emerge

SPONSORED Why are Swift reference types bad for app startup time, and what’s the performance cost of protocol conformances? That’s just a couple of the topics you can learn about on the Emerge blog — written by the app performance experts behind Emerge’s advanced app optimization and monitoring tools, based on their experience of working at companies like Apple, Airbnb, Snap, and Spotify.

Find out more

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.