Sorting in Objective C [duplicate]
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
.
精彩评论