我的代码如下:
-(NSInteger) sortList:(NSMutableArray*)list {
if(!list)
return -1;
//sort list
[list sortUsingSelector:@selector(compare:)];
//do other stuff
}
我的代码经常与*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI sortUsingSelector:]: unrecognized selector sent to instance
列表不为空,绝对包含一些元素。所以我不知道它为什么会崩溃。相反,我尝试使用这段代码进行比较
[list sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {
NSNumber *num1 = obj1;
NSNumber *num2 = obj2;
if([num1 integerValue] <= [num2 integerValue]){
return NSOrderedAscending;
}
else
return NSOrderedDescending;
}];
同样的问题也崩溃了。知道我在这里做错了什么吗?
答案 0 :(得分:1)
这对我来说似乎有用,但它并没有内联排序,而是返回排序列表:
-(NSArray *) sortList:(NSMutableArray*)list {
NSMutableArray *copy = [list mutableCopy];
[copy sortUsingComparator:^NSComparisonResult(id _Nonnull num1, id _Nonnull num2) {
if([num1 integerValue] <= [num2 integerValue]){
return NSOrderedAscending;
}
else
return NSOrderedDescending;
return copy;
}