如何通过特定属性按降序对自定义对象的NSArray进行排序? [英] How to sort NSArray of custom objects by a specific property in descending order?

查看:147
本文介绍了如何通过特定属性按降序对自定义对象的NSArray进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使这段代码按降序排列.这段代码总是给我数组升序:

How do I make this piece of code to order in descending order. This code always gives me the array in ascending order:

NSArray *sortedProductsByStyle = [unsortedProducts sortedArrayUsingComparator: ^(Product *p1, Product *p2) {
        return [p1.productStyle compare:p2.productStyle options:NSNumericSearch];
    }];

我认为使用NSOrderedDescending可以工作,但不能:

I thought that using NSOrderedDescending would work but it didn't:

NSArray *sortedProductsByStyle = [unsortedProducts sortedArrayUsingComparator: ^(Product *p1, Product *p2) {
        return [p1.productStyle compare:p2.productStyle options:NSNumericSearch | NSOrderedDescending];
    }];

有什么想法吗?

推荐答案

仅反转比较顺序怎么样?

How about just inverting the compare order?

NSArray *sortedProductsByStyle = [unsortedProducts sortedArrayUsingComparator: ^(Product *p1, Product *p2) {
    return [p2.productStyle compare:p1.productStyle options:NSNumericSearch];
}];

这篇关于如何通过特定属性按降序对自定义对象的NSArray进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆