NSArray排序 --- sortedArrayUsingDescriptors

 
  1. [dataArray sortedArrayUsingFunction: sortDataArrayByModifyFileDate context:NULL];
  2. NSInteger sortDataArrayByModifyFileDate(id obj1,id obj2, void *context)
  3. {
  4. Nsstring* str1 = (Nsstring*)obj1;
  5. Nsstring* str2 = (Nsstring*)obj2;
  6. Nsstring* dateString1 = [[str1 componentsSeparatedByString:@"$"] objectAtIndex:2];
  7. Nsstring* dateString2 = [[str2 componentsSeparatedByString:@"$"] objectAtIndex:2];
  8. DLog(@">>>>>str1:%@",dateString1);
  9. DLog(@">>>>str2:%@",dateString2);
  10. return ([dateString2 compare:dateString1]);
  11. }

To sort your array of objects you:

  1. setup NSSortDescriptor - use names of your variables as keys to setup descriptor for sorting plus the selector to be executed on those keys
  2. get the array of descriptors using NSSortDescriptor that you've setup
  3. 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:40 

Class A extends NSObject - 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; @end 

Implementation:

#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

 

 

http://zani.iteye.com/blog/1144910

相关文章

我正在用TitaniumDeveloper编写一个应用程序,它允许我使用Ja...
我的问题是当我尝试从UIWebView中调用我的AngularJS应用程序...
我想获取在我的Mac上运行的所有前台应用程序的应用程序图标....
我是一名PHP开发人员,我使用MVC模式和面向对象的代码.我真的...
OSX中的SetTimer在Windows中是否有任何等效功能?我正在使用...
我不确定引擎盖下到底发生了什么,但这是我的设置,示例代码和...