oburan
oburan

Reputation: 31

Swift sort FetchRequest result by Turkish locale

How can I sort fetchResult by Turkish language, turkish characters sorted at the end of results.

let managedObjectContext = (UIApplication.shared.delegate as! AppDelegate).managedObjectContext
    let fetch = NSFetchRequest<Contact>(entityName: "Contact")
    let sortDescriptor = NSSortDescriptor(key: "firstName", ascending: true)
    let sortDescriptors = [sortDescriptor]
    fetch.sortDescriptors = sortDescriptors
    do {
        let list = try managedObjectContext.fetch(fetch)
    } catch {
        fatalError("Failed \(error)")
    }

Upvotes: 2

Views: 111

Answers (1)

oburan
oburan

Reputation: 31

Working code:

let managedObjectContext = (UIApplication.shared.delegate as! AppDelegate).managedObjectContext
let fetch = NSFetchRequest<Contact>(entityName: "Contact")
let sortDescriptor = NSSortDescriptor(key: "firstName", ascending: true, selector: #selector(NSString.localizedCaseInsensitiveCompare))
let sortDescriptors = [sortDescriptor]
fetch.sortDescriptors = sortDescriptors
do {
    let list = try managedObjectContext.fetch(fetch)
} catch {
    fatalError("Failed \(error)")
}

Upvotes: 1

Related Questions