Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>To sort your array of objects you:</p> <ol> <li>setup <code>NSSortDescriptor</code> - use names of your variables as keys to setup descriptor for sorting plus the selector to be executed on those keys</li> <li>get the array of descriptors using <code>NSSortDescriptor</code> that you've setup</li> <li>sort your array based on those descriptors</li> </ol> <p>Here are two examples, one using <code>NSDictionary</code> and <code>NSString/NSNumber</code> values sorting on <code>NSNumber</code>, the other one using custom class with sorting on two <code>NSString</code> fields.</p> <p>Follow <a href="http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Collections/Articles/index.html" rel="noreferrer">Sorting and Filtering NSArray Objects</a> in Cocoa programming topics to see more examples and explanation.</p> <p><strong>Example</strong>:</p> <p><em>This was done on GNUStep it should work the same on Cocoa - the code is exactly the same - I'll try when I sit in front of my Mac:</em></p> <p><strong>First example using <code>NSString</code> and <code>NSNumber</code> values with sorting on <code>NSNumber</code> value:</strong></p> <pre><code>NSString * NAME = @"name"; NSString * ADDRESS = @"address"; NSString * FREQUENCY = @"frequency"; NSString * TYPE = @"type"; NSMutableArray * array = [NSMutableArray array]; NSDictionary * dict; dict = [NSDictionary dictionaryWithObjectsAndKeys: @"Alehandro", NAME, @"Sydney", ADDRESS, [NSNumber numberWithInt:100], FREQUENCY, @"T", TYPE, nil]; [array addObject:dict]; dict = [NSDictionary dictionaryWithObjectsAndKeys: @"Xentro", NAME, @"Melbourne", ADDRESS, [NSNumber numberWithInt:50], FREQUENCY, @"X", TYPE, nil]; [array addObject:dict]; dict = [NSDictionary dictionaryWithObjectsAndKeys: @"John", NAME, @"Perth", ADDRESS, [NSNumber numberWithInt:75], FREQUENCY, @"A", TYPE, nil]; [array addObject:dict]; dict = [NSDictionary dictionaryWithObjectsAndKeys: @"Fjord", NAME, @"Brisbane", ADDRESS, [NSNumber numberWithInt:20], FREQUENCY, @"B", TYPE, nil]; [array addObject:dict]; </code></pre> <hr> <p>Sorting part using descriptors with the Frequency field which is <code>NSNumber</code>:</p> <pre><code>NSSortDescriptor * frequencyDescriptor = [[[NSSortDescriptor alloc] initWithKey:FREQUENCY ascending:YES] autorelease]; id obj; NSEnumerator * enumerator = [array objectEnumerator]; while ((obj = [enumerator nextObject])) NSLog(@"%@", obj); NSArray * descriptors = [NSArray arrayWithObjects:frequencyDescriptor, nil]; NSArray * sortedArray = [array sortedArrayUsingDescriptors:descriptors]; NSLog(@"\nSorted ..."); enumerator = [sortedArray objectEnumerator]; while ((obj = [enumerator nextObject])) NSLog(@"%@", obj); </code></pre> <hr> <p><strong>OUTPUT - sorted by Frequency field:</strong></p> <pre><code>2009-12-04 x[1] {address = Sydney; frequency = 100; name = Alehandro; type = T; } 2009-12-04 x[1] {address = Melbourne; frequency = 50; name = Xentro; type = X; } 2009-12-04 x[1] {address = Perth; frequency = 75; name = John; type = A; } 2009-12-04 x[1] {address = Brisbane; frequency = 20; name = Fjord; type = B; } 2009-12-04 x[1] Sorted ... 2009-12-04 x[1] {address = Brisbane; frequency = 20; name = Fjord; type = B; } 2009-12-04 x[1] {address = Melbourne; frequency = 50; name = Xentro; type = X; } 2009-12-04 x[1] {address = Perth; frequency = 75; name = John; type = A; } 2009-12-04 x[1] {address = Sydney; frequency = 100; name = Alehandro; type = T; } </code></pre> <hr> <hr> <p><strong>Second example with custom class and sorting on two <code>NSString</code> variables.</strong></p> <p>Array to sort (see class <code>A</code> at the bottom):</p> <pre><code>NSMutableArray * array = [NSMutableArray array]; [array addObject:[[A alloc] initWithFirstName:@"Alehandro" lastName:@"Xentro" age:[NSNumber numberWithInt:40]]]; [array addObject:[[A alloc] initWithFirstName:@"John" lastName:@"Smith" age:[NSNumber numberWithInt:30]]]; [array addObject:[[A alloc] initWithFirstName:@"John" lastName:@"Smyth" age:[NSNumber numberWithInt:25]]]; [array addObject:[[A alloc] initWithFirstName:@"Torro" lastName:@"Ola" age:[NSNumber numberWithInt:45]]]; [array addObject:[[A alloc] initWithFirstName:@"Alehandro" lastName:@"Bento" age:[NSNumber numberWithInt:41]]]; [array addObject:[[A alloc] initWithFirstName:@"Alehandro" lastName:@"Axel" age:[NSNumber numberWithInt:41]]]; </code></pre> <hr> <p>The sorting part, sort on lastName then firstName:</p> <pre><code>NSString * LASTNAME = @"lastName"; NSString * FIRSTNAME = @"firstName"; NSSortDescriptor *lastDescriptor = [[[NSSortDescriptor alloc] initWithKey:LASTNAME ascending:YES selector:@selector(localizedCaseInsensitiveCompare:)] autorelease]; NSSortDescriptor *firstDescriptor = [[[NSSortDescriptor alloc] initWithKey:FIRSTNAME ascending:YES selector:@selector(localizedCaseInsensitiveCompare:)] autorelease]; NSArray * descriptors = [NSArray arrayWithObjects:lastDescriptor, firstDescriptor, nil]; NSArray * sortedArray = [array sortedArrayUsingDescriptors:descriptors]; </code></pre> <hr> <p>Print the result:</p> <pre><code>NSLog(@"\nSorted ..."); enumerator = [sortedArray objectEnumerator]; while ((obj = [enumerator nextObject])) NSLog(@"%@", obj); </code></pre> <hr> <p>Result (before and after sorting):</p> <pre><code>2009-12-04 00:52:16.637 x[11375] Alehandro, Xentro, age:40 2009-12-04 00:52:16.644 x[11375] John, Smith, age:30 2009-12-04 00:52:16.644 x[11375] John, Smyth, age:25 2009-12-04 00:52:16.644 x[11375] Torro, Ola, age:45 2009-12-04 00:52:16.645 x[11375] Alehandro, Bento, age:41 2009-12-04 00:52:16.645 x[11375] Alehandro, Axel, age:41 2009-12-04 00:52:16.645 x[11375] Sorted ... 2009-12-04 00:52:16.645 x[11375] Alehandro, Axel, age:41 2009-12-04 00:52:16.645 x[11375] Alehandro, Bento, age:41 2009-12-04 00:52:16.645 x[11375] Torro, Ola, age:45 2009-12-04 00:52:16.645 x[11375] John, Smith, age:30 2009-12-04 00:52:16.645 x[11375] John, Smyth, age:25 2009-12-04 00:52:16.645 x[11375] Alehandro, Xentro, age:40 </code></pre> <p>Class <code>A</code> extends <code>NSObject</code> - nothing special here:</p> <pre><code>#import &lt;Foundation/Foundation.h&gt; @interface A : NSObject { NSString * firstName; NSString * lastName; NSNumber * age; } - (id)initWithFirstName:(NSString*)aFirstName lastName:(NSString*)aLastName age:(NSNumber*)anAge; -(NSString* )description; +(NSString*)action; @end </code></pre> <p>Implementation:</p> <pre><code>#import &lt;Foundation/Foundation.h&gt; #import "A.h" @implementation A - (id)init { return [self initWithFirstName:@"N/A" lastName:@"N/A" age:0]; } - (id)initWithFirstName:(NSString*)aFirstName lastName:(NSString*)aLastName age:(NSNumber*)anAge { self = [super init]; if (!self) return nil; firstName = [aFirstName copy]; lastName = [aLastName copy]; age = [anAge copy]; return self; } - (void)dealloc { [firstName release]; [lastName release]; [age release]; [super release]; } </code></pre> <hr> <pre><code>- (NSString *) description { return [NSString stringWithFormat: @"%@, %@, age:%@", firstName, lastName, age]; } @end </code></pre>
 

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