开发者

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜