Objective c 使用NSInterger值对NSMuatbleArray进行排序
我想对NSMutableArray进行排序,其中包含NSInteger值。我尝试使用此解决方案:Objective c 使用NSInterger值对NSMuatbleArray进行排序,objective-c,ios,nsmutablearray,Objective C,Ios,Nsmutablearray,我想对NSMutableArray进行排序,其中包含NSInteger值。我尝试使用此解决方案: NSArray *sorted = [array sortedArrayUsingSelector:@selector(compare:)]; 它几乎成功了,所以我想问为什么它不能让一切都好起来。以下是数据示例: not sorted = ( 30, 42, 54, 6, 18 ) sorted = ( 18, 30, 42,
NSArray *sorted = [array sortedArrayUsingSelector:@selector(compare:)];
它几乎成功了,所以我想问为什么它不能让一切都好起来。以下是数据示例:
not sorted = (
30,
42,
54,
6,
18
)
sorted = (
18,
30,
42,
54,
6
)
我们可以看到,除了数组末尾的6值之外,它对所有内容都进行了排序
为什么会发生这种情况
多谢各位
完整方法:
- (void) initialize{
NSInteger min = 30;
NSInteger interval = 12;
NSInteger count = floor(60/interval);
for (int i = 0; i < count; i++) {
[array addObject:[NSString stringWithFormat:@"%i",min]];
min +=interval;
if (min > 60)
min -= 60;
}
//DLog(@"not sorted = %@",minutes);
array = [[array sortedArrayUsingSelector:@selector(compare:)] mutableCopy];
//DLog(@"sorted = %@",minutes);
}
-(void)初始化{
NSInteger最小值=30;
NSInteger间隔=12;
NSInteger计数=楼层(60/间隔);
for(int i=0;i60)
min-=60;
}
//DLog(@“未排序=%@”,分钟);
array=[[array-sortedarrayingselector:@selector(compare:)]mutableCopy];
//DLog(@“排序=%@),分钟);
}
在上述情况下,您会遇到问题,因为您排序的是字符串值而不是NSNUMBER。
必须使用NSNumber添加整数对象。然后做一些类似的事情:
NSMutableArray *array = [[NSMutableArray alloc ] initWithObjects:
[NSNumber numberWithInt:10],
[NSNumber numberWithInt:50],
[NSNumber numberWithInt:5],
[NSNumber numberWithInt:3],
nil];
NSLog(@"SORTED = %@",[array sortedArrayUsingSelector:@selector(compare:
)]);
这将为您提供一个排序数组。您对数组做了什么?我可以在我的Mac电脑上得到正确的结果。2012-12-18 16:16:12.936无标题[10001:707](30,42,54,6,18)2012-12-18 16:16:12.938无标题[10001:707](6,18,30,42,54)看看这里你是如何包装NSIntegers的?它们是用NSString、NSValue还是NSNumber包装的?添加了完整方法。用字符串包装