如何使用NSSortDescriptor对NSMutableArray进行排序?

3

我正在从JSON Web服务中解析数据,然后使用以下代码按价格、日期、折扣等对数据进行排序。

这是我用来排序数据的代码:

-(void)priceSort:(id)sender {

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc]
                                     initWithKey: @"price" ascending: YES];

NSMutableArray *sortedArray = (NSMutableArray *)[self.displayItems
                                          sortedArrayUsingDescriptors: [NSArray arrayWithObject:sortDescriptor]];

[self setDisplayItems:sortedArray];

[self.tableView reloadData];


}

这段代码在按价格排序时可以正常工作,但是当我想按评论数量排序时,似乎无法正确使用这段代码:
对于价格,我使用以下代码:
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc]
                                         initWithKey: @"price" ascending: YES];

但是对于评论,我想要获取“n”值。请查看它如何嵌套在下面的可变数组“display Items”的输出中:

"old_price" = 24;
        price = "9.9";
        reviews =         {
            **n = 11;**
            val = 70;
        };
        "sold_count" = 101;

感谢您的帮助:)
1个回答

4
要按评论数量n排序,您的排序描述符应为:
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc]
                                     initWithKey: @"reviews.n" ascending: YES];

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接