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.

   

Save 50% in my Black Friday sale.

SAVE 50% To celebrate WWDC22, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

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.