排序你,你的對象數組:
- 設置
NSSortDescriptor
- 你的變量作爲鍵設置描述符排序加選擇使用的名稱將在琴鍵上執行
- 獲得描述符的數組使用
NSSortDescriptor
你已經設置
- 排序基於這些描述符
這裏有兩個例子,一個使用陣列NSDictionary
和NSString/NSNumber
值排序NSNumber
,另一個使用自定義類與排序在兩個NSString
字段。
按照Sorting and Filtering NSArray Objects在Cocoa編程主題中看到更多的例子和解釋。
例:
這是在GNUstep的做它應該工作一樣可可 - 該代碼是完全一樣的 - 當我坐在我的Mac面前我會嘗試:
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];
:使用
NSString
和
NSNumber
值與
NSNumber
值排序
第一示例
排序使用部分描述符與所述頻率字段,它是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 - 用頻域來分類:
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然後姓:
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);
結果(前和排序後):A
延伸NSObject
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
類 - 這裏沒有什麼特別:
#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
唉!這很難理解。寶貝開發者請在你的問題中使用標點符號(主要是句號和逗號)。 – pmg 2009-12-04 00:24:00
你說這個「frecuency」是一個int值,但你的代碼說它是一個NSString。請注意清楚地說出你的問題。 – Chuck 2009-12-04 00:36:37