NSPredicate排序数组和顺序DESC [英] NSPredicate Sort Array and Order DESC

查看:329
本文介绍了NSPredicate排序数组和顺序DESC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 NSMutableArray 包含 TBPosts ,我想根据<$ c TBPost 的$ c> commentsCount 和 likesCount

I have an NSMutableArray containing TBPosts that I would like to filter in descending order according to the commentsCount and likesCount of the TBPost.

最初,过滤数组中的第一个对象将是具有最大数量的注释和喜欢的对象,这可以通过将两者加在一起来计算。所以我尝试以下查询并收到无法解析错误。请告诉我在哪里出错?

Initially, the first object in the filtered array will be the object with the largest number of comments and likes, which can be worked out by adding the two together. So I tried the following query and receive an Unable to Parse error. Please can you tell me where I am going wrong?

[posts filterUsingPredicate:[NSPredicate predicateWithFormat:@"post.commentsCount + post.likesCount DESC"]];


推荐答案

使用比较器,其格式如下:

Using a comparator, it would look like this:

[posts sortUsingComparator:^NSComparisonResult(id p1, id p2) {
    if (p1.commentsCount + p1.likesCount < p2.commentsCount + p2.likesCount)
        return (NSComparisonResult)NSOrderedAscending;
    if (p1.commentsCount + p1.likesCount > p2.commentsCount + p2.likesCount)
        return (NSComparisonResult)NSOrderedDescending;
    return (NSComparisonResult)NSOrderedSame;

}];

这篇关于NSPredicate排序数组和顺序DESC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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