Master data is sorted by multiple sort descriptors using CaseInsensitiveCompare

I have a subclass of Word: NSManagedObject, which I am trying to group by the first letter of the word. In each section, I then try to sort by the length property, preserving alphanumeric sorting when the words are the same length. So it will look like

A Words

  • apple Length = 5
  • ax Length = 3
  • am Length = 2

B Words

  • bane Length = 4
  • Boat Length = 4
  • Bag Length = 3

So first I launch the new NSFetchRequest. Then I add my sort descriptors, first sorting by value (this is just a word), and then sorting by length. Finally, run my fetchedResultsController and use the group value to group them by the first letter. Here is my code, but I am not getting the desired result. Any help would be greatly appreciated.

@interface Word : NSManagedObject @property (nonatomic, retain) NSString * value; @property (nonatomic, retain) NSString * group; @property (nonatomic, retain) NSNumber * length; @end NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"Word"]; request.sortDescriptors = [NSArray arrayWithObjects:[NSSortDescriptor sortDescriptorWithKey:@"value" ascending:YES selector:@selector(localizedCaseInsensitiveCompare:)], [NSSortDescriptor sortDescriptorWithKey:@"length" ascending:NO], nil]; self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request managedObjectContext:self.wordDatabase.managedObjectContext sectionNameKeyPath:@"group" cacheName:nil]; 
+8
ios5 core-data nsfetchedresultscontroller nssortdescriptor
source share
1 answer

The first sort descriptor must be for the key used as sectionNameKeyPath , the second sort descriptor for the length key and the last for value :

 request.sortDescriptors = [NSArray arrayWithObjects: [NSSortDescriptor sortDescriptorWithKey:@"group" ascending:YES], [NSSortDescriptor sortDescriptorWithKey:@"length" ascending:NO], [NSSortDescriptor sortDescriptorWithKey:@"value" ascending:YES selector:@selector(localizedCaseInsensitiveCompare:)], nil]; 
+22
source share

All Articles