Core Data sortDescriptor with diacritics results in strange behaviour

40 views Asked by At

I have a standard Core Data setup with multiple sortDescriptors

func getPilots() -> [PilotEntity] {
        let request = PilotEntity.fetchRequest()
        let primarySortDescriptor = NSSortDescriptor(key: "isSelf", ascending: false)
        let secondarySortDescriptor = NSSortDescriptor(key: "isFavourite", ascending: false)
        let tertiarySortDescriptor = NSSortDescriptor(key: "name", ascending: true)
        request.sortDescriptors = [primarySortDescriptor, secondarySortDescriptor, tertiarySortDescriptor]
        
        do {
            return try fetch(request)
        } catch {
            print("Error Fetching PilotEntity. \(error)")
            return []
        }
    }

I am using the PilotEntity in the SwiftUI List. I have noticed a strange behaviour when the name contains diacritics. When updating the PilotEntity the list containing names with diacritics are momentarily reshuffled (index 9 is at index 7 and then jumps at 9 where it should be). When the diacritics are removed, the behaviour stops.

1

There are 1 answers

0
Petr Smejkal On

I have added selector to the sort descriptor NSSortDescriptor(key: "name", ascending: true, selector: #selector(NSString.localizedStandardCompare(_:))) to handle the diacritics. This has stopped the unexpected behaviour in the List view.