慕神8447489
要对您的对象数组进行排序:setup NSSortDescriptor- 使用变量名作为设置描述符进行排序的键以及要在这些键上执行的选择器使用NSSortDescriptor您设置的描述符数组根据这些描述符对数组进行排序这里有两个例子,一个使用NSDictionary和NSString/NSNumber值排序NSNumber,另一个使用自定义类,在两个NSString字段上排序。在Cocoa编程主题中按照排序和过滤NSArray对象来查看更多示例和说明。示例:这是在GNUStep上完成的,它应该在Cocoa上运行相同 - 代码完全一样 - 当我坐在我的Mac前面时我会尝试:第一个示例使用NSString和NSNumber值对值进行排序NSNumber: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];使用带有频率字段的描述符对部件进行排序,即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);输出 - 按频率字段排序: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; }第二个示例使用自定义类并对两个NSString变量进行排序。要排序的数组(参见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]]];排序部分,在lastName上排序,然后是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);结果(排序前后):2009-12-04 00:52:16.637 x[11375] Alehandro, Xentro, age:402009-12-04 00:52:16.644 x[11375] John, Smith, age:302009-12-04 00:52:16.644 x[11375] John, Smyth, age:252009-12-04 00:52:16.644 x[11375] Torro, Ola, age:452009-12-04 00:52:16.645 x[11375] Alehandro, Bento, age:412009-12-04 00:52:16.645 x[11375] Alehandro, Axel, age:412009-12-04 00:52:16.645 x[11375]Sorted ...2009-12-04 00:52:16.645 x[11375] Alehandro, Axel, age:412009-12-04 00:52:16.645 x[11375] Alehandro, Bento, age:412009-12-04 00:52:16.645 x[11375] Torro, Ola, age:452009-12-04 00:52:16.645 x[11375] John, Smith, age:302009-12-04 00:52:16.645 x[11375] John, Smyth, age:252009-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 "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