- [dataArray sortedArrayUsingFunction: sortDataArrayByModifyFileDate context:NULL];
- NSInteger sortDataArrayByModifyFileDate(id obj1,id obj2, void *context)
- {
- Nsstring* str1 = (Nsstring*)obj1;
- Nsstring* str2 = (Nsstring*)obj2;
- Nsstring* dateString1 = [[str1 componentsSeparatedByString:@"$"] objectAtIndex:2];
- Nsstring* dateString2 = [[str2 componentsSeparatedByString:@"$"] objectAtIndex:2];
- DLog(@">>>>>str1:%@",dateString1);
- DLog(@">>>>str2:%@",dateString2);
- return ([dateString2 compare:dateString1]);
- }
[dataArray sortedArrayUsingFunction: sortDataArrayByModifyFileDate context:NULL]; NSInteger sortDataArrayByModifyFileDate(id obj1,void *context) { Nsstring* str1 = (Nsstring*)obj1; Nsstring* str2 = (Nsstring*)obj2; Nsstring* dateString1 = [[str1 componentsSeparatedByString:@"$"] objectAtIndex:2]; Nsstring* dateString2 = [[str2 componentsSeparatedByString:@"$"] objectAtIndex:2]; DLog(@">>>>>str1:%@",dateString1); DLog(@">>>>str2:%@",dateString2); return ([dateString2 compare:dateString1]); }
To sort your array of objects you:
- setup
NSSortDescriptor
- use names of your variables as keys to setup descriptor for sorting plus the selector to be executed on those keys - get the array of descriptors using
NSSortDescriptor
that you've setup - sort your array based on those descriptors
Here are two examples,one using NSDictionary
and Nsstring/NSNumber
values sorting onNSNumber
,the other one using custom class with sorting on two Nsstring
fields.
Follow Sorting and Filtering NSArray Objects in Cocoa programming topics to see more examples and explanation.
Example:
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:
First example usingNsstring
and NSNumber
values with sorting on NSNumber
value:
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", @"Melbourne", [NSNumber numberWithInt:50], @"X", nil]; [array addobject:dict]; dict = [NSDictionary dictionaryWithObjectsAndKeys: @"John", @"Perth", [NSNumber numberWithInt:75], @"A", nil]; [array addobject:dict]; dict = [NSDictionary dictionaryWithObjectsAndKeys: @"Fjord", @"Brisbane", [NSNumber numberWithInt:20], @"B", nil]; [array addobject:dict];
Sorting part using descriptors with the Frequency field which is NSNumber
:
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);
OUTPUT - sorted by Frequency field:
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; }
Second example with custom class and sorting on twoNsstring
variables.
Array to sort (see class A
at the bottom):
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]]];
The sorting part,sort on lastName then firstName:
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];
Print the result:
NSLog(@"\nSorted ..."); enumerator = [sortedArray objectEnumerator]; while ((obj = [enumerator nextObject])) NSLog(@"%@",221)">Result (before and after sorting):
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, age:41 2009-12-04 00:52:16.645 x[11375] Torro, age:45 2009-12-04 00:52:16.645 x[11375] John, age:30 2009-12-04 00:52:16.645 x[11375] John, age:25 2009-12-04 00:52:16.645 x[11375] Alehandro, age:40Class
A
extendsNSObject
- nothing special here:#import <Foundation/Foundation.h> @interface A : NSObject { Nsstring * firstName; Nsstring * lastName; NSNumber * age; } - (id)initWithFirstName:(Nsstring*)aFirstName lastName:(Nsstring*)aLastName age:(NSNumber*)anAge; -(Nsstring* )description; +(Nsstring*)action; @endImplementation:
#import <Foundation/Foundation.h> #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]; }
- (Nsstring *) description { return [Nsstring stringWithFormat: @"%@,%@,age:%@", firstName, lastName, age]; }@end