NSArray 排序不是整数格式
NSArray 排序时遇到问题
我在使用
NSArray *ratio = [NSArray arrayWithObjects:
@"10",@"14",@"23",@"21",@"24",@"26",@"26",@"28",@"29",@"0",
@"-11",@"-22",@"-33",@"-44",@"-55",@"-66",
@"-77",@"-88",@"-99",@"-12",@"12",nil];
,
NSSortDescriptor *sortOrder = [NSSortDescriptor sortDescriptorWithKey:@"self" ascending: NO];
NSArray *sortedArray = [ratio sortedArrayUsingDescriptors: [NSArray arrayWithObject:sortOrder]];
NSLog(@"%@",[ratio sortedArrayUsingDescriptors: [NSArray arrayWithObject:sortOrder]]);
NSLog(@"Biggest : %@", [sortedArray objectAtIndex:0]);
NSLog(@"Smallest : %@", [sortedArray lastObject]);
但无法像整数一样排序:( 排序字符串格式如下所示; 9, 9, 9, 8, 7, 7, 6, 43, 4, 4, 31, 30, 29, 29, 28, 28, 27, 27)
我尝试了该代码但给出了错误:( NSArray *sortedArray = [ratiosortedArrayUsingComparator:^(NSString * str1, NSString * str2) {
if ([str1 integerValue] < [str2 integerValue]) {
return (NSComparisonResult)NSOrderedDescending;
}
if ([str1 integerValue] > [str2 integerValue]) {
return (NSComparisonResult)NSOrderedAscending;
}
return (NSComparisonResult)NSOrderedSame;
}];
错误说;正在初始化不兼容的块指针类型.....预期的 NSComparator
i have a problem at NSArray Sorting
i use
NSArray *ratio = [NSArray arrayWithObjects:
@"10",@"14",@"23",@"21",@"24",@"26",@"26",@"28",@"29",@"0",
@"-11",@"-22",@"-33",@"-44",@"-55",@"-66",
@"-77",@"-88",@"-99",@"-12",@"12",nil];
and i used
NSSortDescriptor *sortOrder = [NSSortDescriptor sortDescriptorWithKey:@"self" ascending: NO];
NSArray *sortedArray = [ratio sortedArrayUsingDescriptors: [NSArray arrayWithObject:sortOrder]];
NSLog(@"%@",[ratio sortedArrayUsingDescriptors: [NSArray arrayWithObject:sortOrder]]);
NSLog(@"Biggest : %@", [sortedArray objectAtIndex:0]);
NSLog(@"Smallest : %@", [sortedArray lastObject]);
but can not sort like integer:( sorted string format looks like following ;
9, 9, 9, 8, 7, 7, 6, 43, 4, 4, 31, 30, 29, 29, 28, 28, 27, 27)
i tried that code but gives error :(
NSArray *sortedArray = [ratio sortedArrayUsingComparator:^(NSString * str1, NSString * str2) {
if ([str1 integerValue] < [str2 integerValue]) {
return (NSComparisonResult)NSOrderedDescending;
}
if ([str1 integerValue] > [str2 integerValue]) {
return (NSComparisonResult)NSOrderedAscending;
}
return (NSComparisonResult)NSOrderedSame;
}];
Error says; incompatible block pointer types initializing ..... expected NSComparator
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这很完美:
This works perfectly:
尝试使用
@"intValue"
作为键来代替排序描述符中的@"self"
。Try using
@"intValue"
as the key in place of@"self"
in the sort descriptor.