Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>See the <code>NSMutableArray</code> method <a href="http://developer.apple.com/DOCUMENTATION/Cocoa/Reference/Foundation/Classes/NSMutableArray_Class/Reference/Reference.html#//apple_ref/doc/uid/20000138-BABCEEJD" rel="noreferrer"><code>sortUsingFunction:context:</code></a> </p> <p>You will need to set up a <em>compare</em> function which takes two objects (of type <code>Person</code>, since you are comparing two <code>Person</code> objects) and a <em>context</em> parameter.</p> <p>The two objects are just instances of <code>Person</code>. The third object is a string, e.g. @"birthDate".</p> <p>This function returns an <code>NSComparisonResult</code>: It returns <code>NSOrderedAscending</code> if <code>PersonA.birthDate</code> &lt; <code>PersonB.birthDate</code>. It will return <code>NSOrderedDescending</code> if <code>PersonA.birthDate</code> > <code>PersonB.birthDate</code>. Finally, it will return <code>NSOrderedSame</code> if <code>PersonA.birthDate</code> == <code>PersonB.birthDate</code>.</p> <p>This is rough pseudocode; you will need to flesh out what it means for one date to be "less", "more" or "equal" to another date (such as comparing seconds-since-epoch etc.):</p> <pre><code>NSComparisonResult compare(Person *firstPerson, Person *secondPerson, void *context) { if ([firstPerson birthDate] &lt; [secondPerson birthDate]) return NSOrderedAscending; else if ([firstPerson birthDate] &gt; [secondPerson birthDate]) return NSOrderedDescending; else return NSOrderedSame; } </code></pre> <p>If you want something more compact, you can use ternary operators:</p> <pre><code>NSComparisonResult compare(Person *firstPerson, Person *secondPerson, void *context) { return ([firstPerson birthDate] &lt; [secondPerson birthDate]) ? NSOrderedAscending : ([firstPerson birthDate] &gt; [secondPerson birthDate]) ? NSOrderedDescending : NSOrderedSame; } </code></pre> <p>Inlining could perhaps speed this up a little, if you do this a lot.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload