开发者

Sorting in Objective C [duplicate]

This question already has answers here: Closed 11 years ago.

Possible Duplicate:

How to sort an NSMutableArray with custom objects in it?

I've searched around in google and can't seem to readily find the answer to what I'm looking for, so please do not post links, etc. I'd like to see some actual code.

I am well versed in the code of Java, especially with implementing Comparable to create a "comparable" object which has a compareTo method. In my iPhone application, I have a class called "Contact" which I would like to do开发者_如何学Python the same thing for such that I could somehow easily sort an NSMutableArray full of Contacts. How can I go about doing this?


For a full explanation see this answer from a related question.


In short you basically have to implement…

- (NSComparisonResult)compare:(Contact *)otherContact;

…in your Contact class (order of comparison: self, otherContact).

NSComparisonResult has three possible values: NSOrderedAscending, NSOrderedSame and NSOrderedDescending.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜