我在排序包含NS的NSStrings的NSMUtableArray时遇到一个小问题。 我这样排序
scoreString=[NSString stringWithFormat:@"%lld",GameScore];
[scorearr addObject:scoreString];
NSSortDescriptor* sortOrder = [NSSortDescriptor sortDescriptorWithKey: @"self" ascending: NO];
NSArray *scoreArray= [scorearr sortedArrayUsingDescriptors: [NSArray arrayWithObject: sortOrder]];
scorearr =(NSMutableArray *)ary;
答案 0 :(得分:1)
您正在使用可变数组,因此最好编写
NSSortDescriptor* sortOrder = [NSSortDescriptor sortDescriptorWithKey: @"self" ascending: NO];
[scorearr sortUsingDescriptors:[NSArray arrayWithObject: sortOrder]];
这样你就不会在排序时创建新的数组。
答案 1 :(得分:0)
好的,我完成了这件事,
NSNumber *inrank=[NSNumber numberWithInt:GameScore];
scoreString=[NSString stringWithFormat:@"%lld",GameScore];
[scorearr addObject:inrank];
NSLog(@"%@",scorearr.description);
//sorting array
sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
NSSortDescriptor* sortOrder = [NSSortDescriptor sortDescriptorWithKey: @"self" ascending: NO];
NSArray *scoreArray= [scorearr sortedArrayUsingDescriptors: [NSArray arrayWithObject: sortOrder]];
scorearr =(NSMutableArray *)scoreArray;
NSLog(@"%@",scoreArray.description);