App has contentid
coming in as a number string from a json file:
let contentid: AnyObject! = jsonFeed["contentid"]
let stream:Dictionary = [
"contentId": contentid as! String,
]
It is later saved to [NSManagedObject] with:
var articles = [NSManagedObject]()
let entity = NSEntityDescription.entityForName("Article", inManagedObjectContext: managedContext)
let article = NSManagedObject(entity: entity!, insertIntoManagedObjectContext: managedContext)
article.setValue(stream["contentId"], forKey: "contentid")
articles.append(article)
Finally, I use NSSortDescriptor to have Core Data return entries in numerical ascending order:
let sort = NSSortDescriptor(key: "contentid", ascending: true)
fetchRequest.sortDescriptors = [sort]
But entries 6 - 10 are returned as: 10, 6, 7, 8, 9. What would be the correct method of having these numbers evaluated correctly using NSSortDescriptor?
UPDATE:
For the Swift version, please see Volker's answer below. I ended up using:
let sort = NSSortDescriptor(key: "contentid", ascending: true, selector: "localizedStandardCompare:")
and it evaluated the numbered strings as true integers.
UPDATE: Swift 2:
Selector syntax has changed and no longer accepts objc pointers. Thank you user1828845.
let sort = NSSortDescriptor(key: "contentid", ascending: true, selector: #selector(NSString.localizedStandardCompare(_:)))
contentid
a number or a string internally? – Moonfacedinit(key:ascending:selector:)
and there you can useselector: "localizedStandardCompare:"
as described here for example nshipster.com/nssortdescriptor – Moonfacedlet sort = NSSortDescriptor(key: "contentid", ascending: true, selector: "localizedStandardCompare:")
– Oliveolivegreen