【发布时间】:2015-06-11 13:10:09
【问题描述】:
我有一个数组appd.arrOfDictAppProd,其中有一个键Price,它是一个字符串值。但我想按价格值对这个数组进行排序。所以我从 appd.arrOfDictAppProd 数组中获取 Price 键并将 Price 转换为 String 到 Int,然后制作一个没有任何键的 NSMutableArray newarray。我想在不使用任何 Key 的情况下使用 NSSortDescriptor 对它进行排序,因为在我的 newarray 中没有键。我的代码在这里:
for (int i = 0; i<appd.arrOfDictAppProd.count; i++) {
NSString *price_Value = [[appd.arrOfDictAppProd objectAtIndex:indexPath.row] objectForKey:@"price"];
int price_IntValue = [price_Value intValue];
NSLog(@"int value of prices:%d",price_IntValue);
NSNumber *num = [NSNumber numberWithInteger:price_IntValue];
NSLog(@"number-%@",num);
[newarray addObject:num];
}
NSLog(@"price array:%@",newarray);
NSSortDescriptor *sortDescriptor;
sortDescriptor =[[NSSortDescriptor alloc] initWithKey:@"num"
ascending:YES];
NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
NSArray *sortedArray1;
sortedArray1 = [newarray sortedArrayUsingDescriptors:sortDescriptors];
当我运行我的程序时,在 newarray 我有 14 个值,但它们是相同的,即 3。
【问题讨论】:
-
修复
for循环。您一遍又一遍地获取相同的对象,这就是为什么newArray包含 14 个相同的值。 -
@rmaddy。你能告诉我如何修复for循环..plz帮助
-
将
indexPath.row替换为i。 -
是的。我已经改变了值..现在我门阵列中的 14 个不同的值。@谢谢亲爱的。
标签: objective-c nsmutablearray nssortdescriptor