ios - 我想将图像、名称、价格存储在一个数组中,如果我对价格值进行排序,则名称和图像必须正确排列

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

最佳答案

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

-(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/

相关文章:

ios - 为订购应用程序设置核心数据关系

iPad 上的 Java 网络启动

ios - Objective-C : Is there a built-in way to count the total number of items in a 2d/nested NSArray?

objective-c - dispatch_after - Swift 中的 GCD?

ios - Alamofire 4.0.0 : [String:String] is not convertible to [String : Any] & Request is ambiguous without more context

android - 将我的 APK 和 API 文件提供给我的客户是否安全?

ios - Storyboard UIScrollView 将无法正常工作

ios - 统计某个对象在 JSON 查询中出现的次数

iphone - 添加 key 到 NSDictionary (iPhone SDK)

android - 如果我使用 ReactJS 创建 Web 应用程序,我可以重用代码来使用 React Native 构建移动应用程序吗?