我将这三个数据存储到一个中,如果用户喜欢按价格排序,那么一切都应该是有序的。请帮我显示一下。
imgArr =[[NSArray alloc]init];
nameArr=[[NSArray alloc]init];
priceArr=[[NSArray alloc]init];
我用过的Json脚本是这样的。
{
image = "";
name = Blue;
"option_value_id" = 40;
price = 3;
"price_prefix" = "+";
"product_option_value_id" = 3;
quantity = 300;
subtract = 0;
weight = 3;
"weight_prefix" = "+";
},
{
image = "";
name = Green;
"option_value_id" = 41;
price = 1;
"price_prefix" = "+";
"product_option_value_id" = 1;
quantity = 100;
subtract = 0;
weight = 1;
"weight_prefix" = "+";
},
{
image = "";
name = Yellow;
"option_value_id" = 42;
price = 2;
"price_prefix" = "+";
"product_option_value_id" = 2;
quantity = 200;
subtract = 1;
weight = 2;
"weight_prefix" = "+";
}
最佳答案
将所有这些字典存储在一个数组中,并按任意字段对这个数组进行排序-
-(NSArray *)sortArrayByPrice:(NSArray *)originalArray{
NSSortDescriptor *sortByPrice = [NSSortDescriptor sortDescriptorWithKey:@"price" ascending:YES];
NSArray *sortDescriptors = [NSArray arrayWithObject:sortByPrice];
NSArray *sortedArray = [originalArray sortedArrayUsingDescriptors:sortDescriptors];
return sortedArray;
}
关于ios - 我想将图像、名称、价格存储在一个数组中,如果我对价格值进行排序,则名称和图像必须正确排列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31383140/