如何使用sortedArrayUsingDescriptorssortingNSMutableArray?

我有一个关于sortingNSMutableArray的问题。 我可以使用sortedArrayUsingDescriptors:方法来sorting一个数组与对象。

例如,我有一个NSMutableArrayplaces ,我有一个属性frequency (诠释价值),我想sorting降frequency但我不知道如何正确使用它。

我在initWithKey把什么作为关键字?

我的对象的place包含:

 NSString * name; NSString * address; NSString * frequency; NSString * type; 

 NSMutableArray * places; ... populate array with objects ... NSSortDescriptor * sortByFrequency = [[[NSSortDescriptor alloc] initWithKey:@"????????" ascending:NO] autorelease]; NSArray * descriptors = [NSArray arrayWithObject:sortByFrequency]; NSArray * sorted = [x sortedArrayUsingDescriptors:descriptors]; 

要sorting你的对象数组:

  1. 设置NSSortDescriptor – 使用variables的名称作为关键字来设置sorting描述符以及在这些关键字上执行的select器
  2. 使用您设置的NSSortDescriptor获取描述符数组
  3. 根据这些描述符对数组进行sorting

下面是两个例子,一个使用NSDictionaryNSString/NSNumber值对NSNumbersorting,另一个使用自定义类对两个NSString字段进行sorting。

遵循Cocoa编程主题中的sorting和筛选NSArray对象来查看更多示例和解释。

例如

这是在GNUStep上完成的,它应该和Cocoa一样工作 – 代码完全一样 – 当我坐在我的Mac前面时,我会尝试:

第一个示例使用NSStringNSNumber值对NSNumber值进行sorting:

 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]; 

使用描述符对部分进行sorting,频率字段为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); 

输出 – 按频率sorting:

 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; } 


第二个自定义类和两个NSStringvariablessorting的例子。

要sorting的数组(请参阅底部的类A ):

 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]]]; 

sorting部分,sortinglastName然后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]; 

打印结果:

 NSLog(@"\nSorted ..."); enumerator = [sortedArray objectEnumerator]; while ((obj = [enumerator nextObject])) NSLog(@"%@", obj); 

结果(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, 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 

A类扩展NSObject – 这里没什么特别的:

 #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 

执行:

 #import <Foundation/Foundation.h> #import "Ah" @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 

“键”是你的对象(你的数组“x”)的元素的方法,它返回你想要sorting的东西。 所以在这种情况下,你说你想按“频率”sorting。 那么你所要做的就是使用返回频率的方法的名称作为键。

下面是如何sorting一个NSMutableArray:

 NSMutableArray *numberSort =[[NSMutableArray alloc] init]; while ((key = [enumerator nextObject])) { //(NSNumber *)integer = [key integerValue]; [numberSort addObject:[NSNumber numberWithInt:[key intValue]]]; // code that uses the returned key } NSArray *stringSort = [numberSort sortedArrayUsingSelector:@selector(compare:)]; enumerator = [stringSort objectEnumerator]; NSNumber *intKey; NSMutableArray *backToString =[[NSMutableArray alloc] init]; while ((intKey = [enumerator nextObject])) { //(NSNumber *)integer = [key integerValue]; [backToString addObject:[intKey stringValue]]; // code that uses the returned key