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

}

使用 acsending 对其余部分进行排序(目前的操作方式)

我该怎么做呢?

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

更新代码:

 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
    }];
4

1 回答 1

3

以下是使用自定义逻辑对数组进行排序的方法,并使用字母排序来打破平局:

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];
}];
于 2011-12-12T01:58:57.043 回答