如何使用NSSortDescriptor和复杂的逻辑进行排序?
我有以下按升序排序的代码。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
以下是如何使用自定义逻辑对数组进行排序,并按字母顺序排序以打破平局:
Here is how you sort arrays using custom logic, with alphabetic sorting for tie-breaking: