I have an NSFetchRequest that retrieves an object called "Player", and I want the results to be sorted by 3 attributes in the following order:
- Does the "sendoffOffence" attribute mean zero or not
- Regardless of whether the relation in the object in the team.homeMatch is zero or not
- I want the rows in the number attribute to be sorted in ascending order.
Basically, I’m looking for all the players who have a red card (“sending OffOffence” not zero) to be displayed at the top of the list, and then set the set, regardless of whether the player is in the home team or not, and then finally, get a set of players in the team in the group to combat them, will be sorted by their knitwear numbers.
Thus, I use the following code in my fetch request:
// Set the entity for the fetch request NSEntityDescription *entity = [NSEntityDescription entityForName:@"Player" inManagedObjectContext:self.match.managedObjectContext]; [fetchRequest setEntity:entity]; NSPredicate *predicate = [NSPredicate predicateWithFormat:@"match == %@", self.match]; [fetchRequest setPredicate:predicate]; // Sort the results // Sort according to whether the user has a red card / sendingOffOffence NSSortDescriptor *hasRedCard = [[NSSortDescriptor alloc] initWithKey:@"sendingoffOffence" ascending:NO]; // Sort according to whether the user is on the home team or not NSSortDescriptor *isHomeTeam = [[NSSortDescriptor alloc] initWithKey:@"team.homeMatch" ascending:NO]; // Sort according to the player jersey numbers NSSortDescriptor *number = [[NSSortDescriptor alloc] initWithKey:@"number" ascending:YES selector:@selector(localizedStandardCompare:)]; [fetchRequest setSortDescriptors:@[hasRedCard, isHomeTeam, number]]; // Set the amount of records to retrieve from the database [fetchRequest setFetchBatchSize:20]; NSFetchedResultsController *theFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.match.managedObjectContext sectionNameKeyPath:nil cacheName:@"MatchCache"]
However, when I execute the above code, I get the following results:


This sort order is incorrect because I want the following order to appear in the red card section:
- 11 - Home
- 12 - Home
- 0 - Away
- 11 - Away
- 21 - Away
And in the yellow card section:
- 1 - Home
- 2 - Home
- 99 - Home
- 1 - Away
- 31 - Away
It looks like the yellow card section is sorted correctly, but the red card section shows very strange behavior, which makes it seem like it is not sorted at all.
I'm pretty surprised why the red card is not sorted correctly - any thoughts? Should I just sort these objects in memory instead of relying on basic data to get my preferred order?
Note that this is the main data application with a repository of stored SQL repositories.
UPDATE
The following is the SQL statement that kernel data uses in my sample:
CoreData: annotation: fetch using NSSQLiteStatement <0x11c77cd0> on entity 'SPlayer' with sql text 'SELECT t0.Z_ENT, t0.Z_PK FROM ZFSMANAGEDOBJECT t0 LEFT OUTER JOIN ZSTEAM t1 ON t0.ZTEAM = t1.Z_PK WHERE ( t0.ZMATCH1 = ? AND t0.Z_ENT = ?) ORDER BY t0.ZSENDINGOFFOFFENCE DESC, t1.ZHOMEMATCH DESC, t0.ZNUMBER COLLATE NSCollateFinderlike '