I want to fetch Objects in Ascending order from core data. The problem is, I want the objects as
Abc ,abc Ball, ball bat, Cat, can
But, it sends like,
Abc,Ball,Cat ,abc, ball, cat
Following is my code.
class func CD_FetchAllContacts()->[RecentContact]{
var arrResult:[RecentContact] = []
let managedContext = PM.instance().managedObjectContext
let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "RecentContact")
fetchRequest.sortDescriptors = [NSSortDescriptor(key: "firstName", ascending: true, selector: #selector(NSString.caseInsensitiveCompare))]
do {
let results =
try managedContext?.fetch(fetchRequest)
arrResult = results as! [RecentContact]
} catch let error as NSError {
print("Could not fetch \(error), \(error.userInfo)")
}
print(arrResult.count)
return arrResult
}
The sort descriptor should be like this, with (_ :)
at the end:
fetchRequest.sortDescriptors = [NSSortDescriptor(key: "firstName", ascending: true, selector: #selector(NSString.caseInsensitiveCompare(_:)))]
Edit: Actually even with your version of the sort descriptor it's working on a test project I've done. Nothing seems wrong with the code posted. Are you sorting the returned array in other part of your code?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.