1
votes

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 sorting is a little more difficult. Is there some kind of class method to help in this case? How should it be done?

1

1 Answers

4
votes

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]];