Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/116.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 对嵌套的NSMutableArray排序_Ios_Nsmutablearray - Fatal编程技术网

Ios 对嵌套的NSMutableArray排序

Ios 对嵌套的NSMutableArray排序,ios,nsmutablearray,Ios,Nsmutablearray,我要排序的NSMutableArray如下所示: ( { "title" = "Bags"; "price" = "$200"; }, { "title" = "Watches"; "price" = "$40"; }, { "title" = "Earrings"; "p

我要排序的
NSMutableArray
如下所示:

 (
        {
            "title" = "Bags";
            "price" = "$200";
        },
        {
            "title" = "Watches";
            "price" = "$40";
        },
        {
            "title" = "Earrings";
            "price" = "$1000";
        }
 )
它是一个
NSMutableArray
,包含
NSMutableArray
s的集合。我想先按
价格
排序,然后按
标题
排序

NSSortDescriptor *sortByPrices = [[NSSortDescriptor alloc] initWithKey:@"price" ascending:YES];
NSSortDescriptor *sortByTitle = [[NSSortDescriptor alloc] initWithKey:@"title" ascending:YES];

[arrayProduct sortedArrayUsingDescriptors:[NSArray arrayWithObjects:sortByPrices,sortByTitle,nil]];

然而,这似乎不起作用,如何对嵌套的
NSMutableArray
进行排序

我想错误在于
price
是一个字符串。因此,它不是在数字上进行比较,而是在词典上进行比较。尝试使用比较器块对数组进行排序,并在该块内解析价格:

[array sortUsingComparator:^(id _a, id _b) {
    NSDictionary *a = _a, *b = _b;

    // primary key is the price
    int priceA = [[a[@"price"] substringFromIndex:1] intValue];
    int priceB = [[b[@"price"] substringFromIndex:1] intValue];

    if (priceA < priceB)
        return NSOrderedAscending;
    else if (priceA > priceB)
        return NSOrderedDescending;
    else // if the prices are the same, sort by name
        return [a[@"title"] compare:b[@"title"]];
}];
[array sortUsingComparator:^(id\u a,id\u b){
NSDictionary*a=\u a,*b=\u b;
//主要的关键是价格
int price a=[[a[@“price”]substringfromtindex:1]intValue];
intpriceb=[[b[@“price”]substringfromtindex:1]intValue];
如果(价格A<价格B)
回报率下降;
否则如果(价格A>价格B)
退而求其次;
else//如果价格相同,请按名称排序
返回[a[@“title”]比较:b[@“title”];
}];

我想错误在于
price
是一个字符串。因此,它不是在数字上进行比较,而是在词典上进行比较。尝试使用比较器块对数组进行排序,并在该块内解析价格:

[array sortUsingComparator:^(id _a, id _b) {
    NSDictionary *a = _a, *b = _b;

    // primary key is the price
    int priceA = [[a[@"price"] substringFromIndex:1] intValue];
    int priceB = [[b[@"price"] substringFromIndex:1] intValue];

    if (priceA < priceB)
        return NSOrderedAscending;
    else if (priceA > priceB)
        return NSOrderedDescending;
    else // if the prices are the same, sort by name
        return [a[@"title"] compare:b[@"title"]];
}];
[array sortUsingComparator:^(id\u a,id\u b){
NSDictionary*a=\u a,*b=\u b;
//主要的关键是价格
int price a=[[a[@“price”]substringfromtindex:1]intValue];
intpriceb=[[b[@“price”]substringfromtindex:1]intValue];
如果(价格A<价格B)
回报率下降;
否则如果(价格A>价格B)
退而求其次;
else//如果价格相同,请按名称排序
返回[a[@“title”]比较:b[@“title”];
}];
试试这个

这很好地帮助你

试试这个

这会帮助您尝试

    NSMutableArray  *arrayProducts = [@[@{@"price":@"$200",@"title":@"Bags"},@{@"price":@"$40",@"title":@"Watches"},@{@"price":@"$1000",@"title":@"Earrings"}] mutableCopy];

    NSSortDescriptor *priceDescriptor = [NSSortDescriptor sortDescriptorWithKey:@""
                                                                 ascending:YES
                                                                comparator:^NSComparisonResult(NSDictionary  *dict1, NSDictionary *dict2) {
                                                                    return [dict1[@"price"] compare:dict2[@"price"] options:NSNumericSearch];
    }];

    NSSortDescriptor *titleDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"title" ascending:YES];



    [arrayProducts sortUsingDescriptors:@[priceDescriptor,titleDescriptor]];

    NSLog(@"SortedArray : %@",arrayProducts);
试一试


它是如何工作的,你之前和之后都有什么,你是如何进行排序的?它看起来更像是内部带有字典的数组。它是如何工作的,之前和之后都有什么,你是如何进行排序的?它看起来更像是带有字典的数组inside@Desdenova谢谢,我不知道是怎么错过的。直接从xcode复制。'*'在'dict1'之前缺失-->比较器:^n比较结果(NSDictionary dict1,NSDictionary*dict2){@Desdenova谢谢,不知道它是如何丢失的。直接从xcode复制的。'*'在'dict1'之前丢失-->比较器:^n比较结果(NSDictionary dict1,NSDictionary*dict2){