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
}];
Here is how you sort arrays using custom logic, with alphabetic sorting for tie-breaking: