sortedarrayusingdescriptors
似乎没有像我希望的那样进行就地排序。
看起来它需要一个额外的数组来进行排序,然后你将清除你原来的可变数组,并用排序数组的结果重新填充它。
例如,我希望这可以工作......
@interface SomeClass
{
NSMutableArray *mutableArrayOfItems;
}
-(void)sortItems
{
NSSortDescriptor *descriptor1 = [[NSSortDescriptor alloc]initWithKey:@"name" ascending:YES];
NSSortDescriptor *descriptor2 = [[NSSortDescriptor alloc]initWithKey:@"age" ascending:NO];
NSArray *descriptors = [NSArray arrayWithObjects:descriptor1,descriptor2, nil];
mutableArrayOfItems = [mutableArrayOfItems sortedArrayUsingDescriptors:descriptors];
}
但不得不这样做....
@interface SomeClass
{
NSMutableArray *mutableArrayOfItems;
}
-(void)sortItems
{
NSSortDescriptor *descriptor1 = [[NSSortDescriptor alloc]initWithKey:@"name" ascending:YES];
NSSortDescriptor *descriptor2 = [[NSSortDescriptor alloc]initWithKey:@"age" ascending:NO];
NSArray *descriptors = [NSArray arrayWithObjects:descriptor1,descriptor2, nil];
NSArray *sortedArray = [mutableArrayOfItems sortedArrayUsingDescriptors:descriptors];
[mutableArrayOfItems removeAllObjects];
[mutableArrayOfItems addObjectsFromArray:sortedArray];
}
答案 0 :(得分:1)
那是因为您使用了创建新数组的方法。那么调用sortUsingDescriptors:相反?