我有一个关于NSMutableArray排序的问题。我可以使用sortedArrayUsingDescriptors:方法对带有对象的数组进行排序。
例如,我有一个属性NSMutableArray of places,其中有一个属性frequency (int值),我想在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];发布于 2009-12-04 00:34:56
下面是如何对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 https://stackoverflow.com/questions/1844031
复制相似问题