gpt4 book ai didi

ios - 对嵌套的 NSMutableArray 进行排序

转载 作者:行者123 更新时间:2023-11-28 18:22:20 25 4
gpt4 key购买 nike

我要排序的 NSMutableArray 如下所示:

 (
{
"title" = "Bags";
"price" = "$200";
},
{
"title" = "Watches";
"price" = "$40";
},
{
"title" = "Earrings";
"price" = "$1000";
}
)

它是一个 NSMutableArray,它包含一组 NSMutableArray。我想先按 price 排序,然后按 title 排序。

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

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

但是,这似乎不起作用,如何对嵌套的 NSMutableArray 进行排序?

最佳答案

尝试

    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);

关于ios - 对嵌套的 NSMutableArray 进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17965289/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com