I don’t have a solution based on NSPredicate or perhaps as elegant as you hoped for, but I ran into the same problem and wrote my own solution, and in fact it wasn’t.
My solution is for a game in which there can only be two participants, so modify them accordingly, but here is the code that I ended up using:
[myGamesArray sortUsingComparator:^NSComparisonResult(CHGame *game1, CHGame *game2) { if (YES == [game1 localPlayersTurn] && NO == [game2 localPlayersTurn]) { return NSOrderedAscending; } else if (NO == [game1 localPlayersTurn] && YES == [game2 localPlayersTurn]) { return NSOrderedDescending; } NSDate *lm1 = [game1.match lastMove]; NSDate *lm2 = [game2.match lastMove]; if (lm1 != nil && lm2 != nil) { return [lm1 compare:lm2]; } return NSOrderedSame; }];
where CHGame is a custom class that I built for my games (which have the GKTurnBasedMatch match property), and the localPlayersTurn instance localPlayersTurn returns a BOOL indicating whether it is a local member rotate or not.
And then I wrote the lastMove method in the category on GKTurnBasedMatch :
- (NSDate *)lastMove { GKTurnBasedParticipant *localParticipant, *otherParticipant; NSDate *lastMove; for (GKTurnBasedParticipant *participant in self.participants) { if (YES == [participant.playerID isEqualToString:[GKLocalPlayer localPlayer].playerID]) { localParticipant = participant; } else { otherParticipant = participant; } } if (localParticipant == self.currentParticipant) { lastMove = otherParticipant.lastTurnDate; } else { lastMove = localParticipant.lastTurnDate; } return lastMove; }
Again, this only works for two participants, but they are easy to change for any number.
Hope this helps, although this is not quite what you asked for.
source share