Sorting in Objective C

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 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 .

链接地址: http://www.djcxy.com/p/70824.html

上一篇: 如何排序NSMutableArray

下一篇: 目标C中的排序