Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8099633
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T22:28:28+00:00 2026-06-05T22:28:28+00:00

I know how to sort Core Data objects in a tableview by NsDate, but

  • 0

I know how to sort Core Data objects in a tableview by NsDate, but this by default seems to create a new section for each object. I want to sort them by a medium formatted date with NSDateFormatter. How would I do this?

For example, if I have 3 objects created on the same day, I want them to be in the same section with the section title being that Day, no time needed.

Each object has an NSDate property. Thanks for your help.

This is the code I have in fetchedResultsController with rgeorge’s suggestions. What am I missing here?

- (NSFetchedResultsController *)fetchedResultsController {

if (fetchedResultsController != nil) {
    NSLog(@"get old fetched controller");

    return fetchedResultsController;
}

else{
    NSLog(@"get new fetched controller");
}

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"InTextEntity" inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];
[fetchRequest setFetchBatchSize:20];

NSSortDescriptor *dateDescriptor = [[NSSortDescriptor alloc] initWithKey:@"dateModified" ascending:NO];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:dateDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];


NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:managedObjectContext sectionNameKeyPath:@"mediumFormattedDate" cacheName:@"Root"];
aFetchedResultsController.delegate = self;
self.fetchedResultsController = aFetchedResultsController;

NSError *error = nil;
if (![fetchedResultsController performFetch:&error]) {

    NSLog(@"Unresolved error %@, %@", error, [error userInfo]);

}

return fetchedResultsController;

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T22:28:29+00:00Added an answer on June 5, 2026 at 10:28 pm

    (I’ll write this up assuming you’re using an NSFetchedResultsController to drive your tableview. If you’re not, I recommend checking it out.)

    An interesting feature of NSFetchedResultsController‘s sectioning abilities: although the property you sort on must be a modeled property (because sqlite does the actual sorting), the property you group the sections with need not be. The only requirement is that the grouping be consistent with the ordering. (i.e., sorting by the sort property will put the objects with matching group properties next to each other.)

    So just add something like this to your modeled object class:

    // in interface
    @property (nonatomic, readonly) NSString *mediumFormattedDate;
    
    // in impl
    -(NSString *)mediumFormattedDate
    {
      // this can be fancier if you need a custom format or particular timezone: 
      return [NSDateFormatter localizedStringFromDate:self.date
                                            dateStyle:NSDateFormatterMediumStyle
                                            timeStyle:NSDateFormatterNoStyle];
    }
    

    (no need to mention mediumFormattedDate in the .xcdatamodel at all.)

    Then go ahead and sort your objects by the date property, but group them by your new property. When you create your NSFetchedResultsController, do so along these lines:

    NSFetchRequest *fr = [NSFetchRequest fetchRequestWithEntityName:@"MyFancyEntity"];
    NSSortDescriptor *sd = [NSSortDescriptor sortDescriptorWithKey:@"date"
                                                         ascending:YES];
    [fr setSortDescriptors:[NSArray arrayWithObject:sd]];
    NSFetchedResultsController *frc =
    [[NSFetchedResultsController alloc] initWithFetchRequest:fr
                                        managedObjectContext:myManagedObjectContext
                                          sectionNameKeyPath:@"mediumFormattedDate"
                                                   cacheName:nil];
    // then do stuff with frc
    

    That’s all it takes! I’ve done this in a few apps to get date grouping and it works well.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to sort the data of a core data NSSet (I know
I know this sort of code is not best practice, but nevertheless in certain
I know that this sort of question has been asked here before, but still
(I know this is a sort of lazy question, but it's oddly hard to
I've got a Core Data setup where each object has an NSDate, and a
I know this sort of question has been banded about before but I've not
I have a core data application which displays a list of custom objects, each
I know that The Treeset Sort the input automatically but don't accept duplicates.Is there
I am working on this core data app, and im not exactly shure how
My Core Data model has an NSDate attribute like here: @interface DBVial : NSManagedObject

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.