0

I use NSFetchedResultsController to fetch from database with Core Data. And I have an Entity with 2 properties, prop1 and prop2 of NSString.
How would I sort sections not only by one of the properties but both?
Now it is:

Title1ForProp1/Title2ForProp2 (prop1==1 prop2==2)
Title1ForProp1/Title1ForProp2 (prop1==1 prop2==1)
Title2ForProp1/Title1ForProp2 (prop1==2 prop2==1)

I need:

Title1ForProp1/Title1ForProp2 (prop1==1 prop2==1)
Title1ForProp1/Title2ForProp2 (prop1==1 prop2==2)
Title2ForProp1/Title1ForProp2 (prop1==2 prop2==1)
surlac
  • 2,961
  • 2
  • 22
  • 31

2 Answers2

3

When you create the fetch request for the NSFC you create the sort descriptor and give the request an array.

You can put as many sort descriptors in the array as you like.

Just create a sort descriptor for each field you want to sort by.

I can remember which order you have to put them into the array though.

OK, so code wise...

NSSortDescriptor *sd1 = [[NSSortDescriptor alloc] initWithKey:@"prop1" ascending:YES];
NSSortDescriptor *sd2 = [[NSSortDescriptor alloc] initWithKey:@"prop2" ascending:YES];

[fetchRequest setSortDescriptors:@[sd1, sd2]];

This is all you have to do.

The NSFC will only split them into sections if you give it a sectionNameKeyPath. If you don't want any sections then make the sectionNameKeyPath nil.

Fogmeister
  • 76,236
  • 42
  • 207
  • 306
  • please see [reference](http://developer.apple.com/library/ios/#documentation/CoreData/Reference/NSFetchedResultsController_Class/Reference/Reference.html#//apple_ref/occ/instm/NSFetchedResultsController/initWithFetchRequest:managedObjectContext:sectionNameKeyPath:cacheName:). "If the controller generates sections, the first sort descriptor in the array is used to group the objects into sections;". But I need to sort it by 2 properties, maybe there is workaround exists. – surlac Feb 04 '13 at 06:14
  • Added the code you need to do. You haven't given any actual details on what you are doing or what you have tried but put this in there (at least try what I said before saying it's wrong). This should do what you asked. – Fogmeister Feb 04 '13 at 07:45
  • I've created simplified example, and run it with code you suggested, and it works! I've found a bug in my current project, which looked like the second property is ignored. But why Apple says that only first descriptor is used for grouping? – surlac Feb 04 '13 at 09:26
  • 1
    I think you mis-interpretted what they're saying. If you have the sectionNameKeyPath set then the first sort descriptor will group them by that property. This is what it's doing anyway but with a sectionNameKeyPath it will break the grouping into different sections. You still have the grouping but not broken into sections. – Fogmeister Feb 04 '13 at 09:28
1

Fogmeister's sort descriptors array is appropriate (sort on prop1 then prop2) but if you provide prop1 in your sectionNameKeyPath your sections would be broken up only by prop1. Within each section, the items would be sorted by both prop1 and prop2.

If this is not what you want and you need to additionally group your results into sections by both prop1 and prop2, you probably want to create a transient property that concatenates both prop1 and prop2 and provide that transient property as your sectionNameKeyPath. This provides not just the title for the section but also determines how results are grouped into sections.

Take a look at this question for how you might create a transient property for your section names: NSFetchedResultsController with sections created by first letter of a string

Community
  • 1
  • 1
nioq
  • 3,215
  • 1
  • 23
  • 18