我想将图像,名称,价格存储在一个数组中,如果我对价格值进行排序,那么名称和图像必须妥善安排

时间:2015-07-13 12:14:17

标签: ios objective-c nsdictionary

我将这三个数据存储到一个,如果用户喜欢按价格排序,那么一切都应该按顺序排列。请帮我这样展示。

  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" = "+";
    }

1 个答案:

答案 0 :(得分:3)

将所有这些词典存储在一个数组中,并按任何字段对此数组进行排序 -

-(NSArray *)sortArrayByPrice:(NSArray *)originalArray{
     NSSortDescriptor *sortByPrice = [NSSortDescriptor sortDescriptorWithKey:@"price" ascending:YES];
     NSArray *sortDescriptors = [NSArray arrayWithObject:sortByPrice];
     NSArray *sortedArray = [originalArray sortedArrayUsingDescriptors:sortDescriptors];
     return sortedArray;
 }