How to sort an array of objects by their NSString property
I have an array of custom objects. One of the properties of these objects is an NSString. I want to sort by that property.
Because it is an NSString and not an NSNumber s开发者_如何学Pythonorting is a little more difficult. Is there some kind of class method to help in this case? How should it be done?
There is indeed. NSArray has a -sortedArrayUsingDescriptors: method. Call this, and pass an array of one sort descriptor for your property. For example, if your property is "lastName", then:
NSSortDescriptor *desc = [[[NSSortDescriptor alloc] initWithKey: @"lastName" ascending: YES];
[array sortedArrayUsingDescriptors: [NSArray arrayWithObject: desc]];
精彩评论