问题描述
我有一个关于排序 NSMutableArray
的问题。我可以使用 sortedArrayUsingDescriptors:
方法对对象排序数组。
例如,我有一个 NSMutableArray
places
我有一个属性频率
(int值),我想按照频率
降序排序,但我现在不现在if
initWithKey
中输入什么? strong> 我的对象地方
包含:
NSString * name;
NSString * address;
NSString * frequency;
NSString * type;
NSMutableArray *地方;
... populate array with objects ...
NSSortDescriptor * sortByFrequency =
[[[NSSortDescriptor alloc] initWithKey:@? ascending:NO] autorelease];
NSArray * descriptors = [NSArray arrayWithObject:sortByFrequency];
NSArray * sorted = [x sortedArrayUsingDescriptors:descriptors];
>
- 设置
NSSortDescriptor
- 使用变量的名称作为排序的设置描述符要在这些键上执行 - 使用您设置的
NSSortDescriptor
获取描述符数组 - 根据这些描述符对数组进行排序
下面是两个示例,一个使用 NSDictionary
和 NSString / NSNumber
值排序 NSNumber
,另一个使用自定义类, code> NSString 字段。
按照在Cocoa编程主题中查看更多的例子和解释。
:
这样做是在GNUStep上完成的,完全相同 - 当我坐在我的Mac前面时,我会尝试:
第一个例子使用 NSString
和 NSNumber
值排序 NSNumber
value: b
$ b
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(@\\\
Sorted ...);
enumerator = [sortedArray objectEnumerator];
while((obj = [enumerator nextObject]))NSLog(@%@,obj);
OUTPUT-按频率字段排序:
2009-12-04 x [1] {address = Sydney;频率= 100。 name = Alehandro;类型= T; }
2009-12-04 x [1] {address = Melbourne;频率= 50; name = Xentro; type = X; }
2009-12-04 x [1] {address = Perth;频率= 75; name = John; type = A; }
2009-12-04 x [1] {address = Brisbane;频率= 20; name = Fjord; type = B; }
2009-12-04 x [1]
Sorted ...
2009-12-04 x [1] {address = Brisbane;频率= 20; name = Fjord; type = B; }
2009-12-04 x [1] {address = Melbourne;频率= 50; name = Xentro; type = X; }
2009-12-04 x [1] {address = Perth;频率= 75; name = John; type = A; }
2009-12-04 x [1] {address = Sydney;频率= 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
升序:YES
选择器:@selector(localizedCaseInsensitiveCompare :)] autorelease ];
NSArray * descriptors =
[NSArray arrayWithObjects:lastDescriptor,firstDescriptor,nil];
NSArray * sortedArray =
[array sortedArrayUsingDescriptors:descriptors];
打印结果:
NSLog(@\\\
Sorted ...);
enumerator = [sortedArray objectEnumerator];
while((obj = [enumerator nextObject]))NSLog(@%@,obj);
结果(排序前后) p>
2009-12-04 00:52:16.637 x [11375] Alehandro,Xentro,年龄:40
2009-12 -04 00:52:16.644 x约翰·史密斯年龄:30
2009-12-04 00:52:16.644 x约翰·史密斯,年龄:25
2009-12 -04 00:52:16.644 x [11375] Torro,Ola,年龄:45
2009-12-04 00:52:16.645 x [11375] Alehandro,Bento,年龄:41
2009-12 -04 00:52:16.645 x [11375] Alehandro,Axel,年龄:41
2009-12-04 00:52:16.645 x $ 113 $ b排序...
2009- 12-04 00:52:16.645 x [11375] Alehandro,Axel,年龄:41
2009-12-04 00:52:16.645 x [11375] Alehandro,Bento,年龄:41
2009- 12-04 00:52:16.645 x [11375] Torro,Ola,年龄:45
2009-12-04 00:52:16.645 x [11375] John,Smith,年龄:30
2009- 12-04 00:52:16.645 x约翰,史密斯,年龄:25
2009-12-04 00:52:16.645 x [11375] Alehandro,Xentro,年龄:40
类 A
extends 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>
#importAh
@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
I have a question about sorting NSMutableArray
. I can use sortedArrayUsingDescriptors:
method to sort an array with objects.
For example I have an NSMutableArray
of places
where I have an attribute frequency
(int value) and I want to sort descending on frequency
but I don't now if I how to use it correctly.
What do I put as a key in initWithKey
?
My object place
contains:
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];
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 on NSNumber
, 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 using NSString
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", 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 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 two NSString
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(@"%@", obj);
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, 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
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
这篇关于如何排序NSMutableArray使用sortedArrayUsingDescriptors?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!