如何使用NSSortDescriptor和复杂的逻辑进行排序?

发布于 2024-12-20 20:11:22 字数 1805 浏览 1 评论 0原文

我有以下按升序排序的代码。

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"someproperty.name" ascending:YES];
    NSMutableArray   *sortedReleases = [NSMutableArray arrayWithArray:[unsortedarray sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]]];
    [sortDescriptor release];

我想要做的是进行排序,其中:

显示当前活动用户后面紧随sortedRelease的排序(函数中的复杂逻辑??)

这就是我的自定义函数中需要的:

for (Release *release in sortedReleases){

if( [[[MainController sharedMainController] activeUser] isFollowingRelease:release] ){

return NSOrderedAscending;
}

}

使用升序对其余部分进行排序(目前它是如何做的)

我将如何做到这一点?

我知道我之前问过这个问题,也许我问错了,但这不是我想要的。我希望能够根据函数的结果进行排序。然后按字母顺序。

更新的代码:

 NSArray *sortedReleases = [theReleases sortedArrayUsingComparator:^(id a, id b) {
        Release *left = (Release*)a;
        Release *right = (Release*)b;


        if(( [[[MainController sharedMainController] activeUser] isFollowingRelease:left] )&&([[[MainController sharedMainController] activeUser] isFollowingRelease:right])){

           //sort alphabetically ????
        }
        else if (([[[MainController sharedMainController] activeUser] isFollowingRelease:left])&&(![[[MainController sharedMainController] activeUser] isFollowingRelease:right]))
        {
            return (NSComparisonResult)NSOrderedDescending;
        }
        else if ((![[[MainController sharedMainController] activeUser] isFollowingRelease:left])&&([[[MainController sharedMainController] activeUser] isFollowingRelease:right]))
        {
             return (NSComparisonResult)NSOrderedAscending;
        }

        return [left compare:right]; //getting a warning here about incompatible types
    }];

I have the following code that sorts by ascending.

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"someproperty.name" ascending:YES];
    NSMutableArray   *sortedReleases = [NSMutableArray arrayWithArray:[unsortedarray sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]]];
    [sortDescriptor release];

What I want to do is do a sort where:

Show the ones where a sortedRelease is being followed by active current user (complex logic that goes in a function??)

This is what I need in my custom function:

for (Release *release in sortedReleases){

if( [[[MainController sharedMainController] activeUser] isFollowingRelease:release] ){

return NSOrderedAscending;
}

}

Sort the rest using acsending (how it is doing it currently)

How would I get about doing that?

I know I asked this question earlier and maybe I asked it the wrong way but that was not what i was looking for. I want to be able to sort based on a result of a function. And then alphabetically.

UPDATED CODE:

 NSArray *sortedReleases = [theReleases sortedArrayUsingComparator:^(id a, id b) {
        Release *left = (Release*)a;
        Release *right = (Release*)b;


        if(( [[[MainController sharedMainController] activeUser] isFollowingRelease:left] )&&([[[MainController sharedMainController] activeUser] isFollowingRelease:right])){

           //sort alphabetically ????
        }
        else if (([[[MainController sharedMainController] activeUser] isFollowingRelease:left])&&(![[[MainController sharedMainController] activeUser] isFollowingRelease:right]))
        {
            return (NSComparisonResult)NSOrderedDescending;
        }
        else if ((![[[MainController sharedMainController] activeUser] isFollowingRelease:left])&&([[[MainController sharedMainController] activeUser] isFollowingRelease:right]))
        {
             return (NSComparisonResult)NSOrderedAscending;
        }

        return [left compare:right]; //getting a warning here about incompatible types
    }];

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ゞ记忆︶ㄣ 2024-12-27 20:11:22

以下是如何使用自定义逻辑对数组进行排序,并按字母顺序排序以打破平局:

NSArray *sortedArray = [unsortedArray sortedArrayUsingComparator:^(id a, id b) {
    Release *left = (Release*)a;
    Release *right = (Release*)b;
    BOOL isFollowingLeft = [[[MainController sharedMainController] activeUser] isFollowingRelease:left];
    BOOL isFollowingRight = [[[MainController sharedMainController] activeUser] isFollowingRelease:right];
    if (isFollowingLeft && !isFollowingRight) {
        return (NSComparisonResult)NSOrderedDescending;
    } else if (!isFollowingLeft && isFollowingRight) {
        return (NSComparisonResult)NSOrderedDescending;
    }
    return [left.name compare:right.name];
}];

Here is how you sort arrays using custom logic, with alphabetic sorting for tie-breaking:

NSArray *sortedArray = [unsortedArray sortedArrayUsingComparator:^(id a, id b) {
    Release *left = (Release*)a;
    Release *right = (Release*)b;
    BOOL isFollowingLeft = [[[MainController sharedMainController] activeUser] isFollowingRelease:left];
    BOOL isFollowingRight = [[[MainController sharedMainController] activeUser] isFollowingRelease:right];
    if (isFollowingLeft && !isFollowingRight) {
        return (NSComparisonResult)NSOrderedDescending;
    } else if (!isFollowingLeft && isFollowingRight) {
        return (NSComparisonResult)NSOrderedDescending;
    }
    return [left.name compare:right.name];
}];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文