I’ve got data in an NSSet, and I need to get it into an NSArray.
Do I need to sort it myself (again, this came from Core Data) or can I get it out in a sorted order?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You can specify a sort when you retrieve data with an
NSFetchRequestby setting thesortDescriptorsproperty to an array ofNSSortDescriptors. But if you already have it in anNSSetand don’t want to make another fetch request, you can use:It’ll create an interim
NSArraywhen you callallObjectsand then another sorted array afterwards, so it’s not 100% efficient, but the overhead should be negligible for reasonably-sized data sets (and certainly less than the cost of sorting).Edit
Actually, I was wrong – NSSet has the
sortedArrayUsingDescriptors:method too. So you can just call[theSet sortedArrayUsingDescriptors:descriptors]and do it all in one go.