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 8262739
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T03:51:23+00:00 2026-06-08T03:51:23+00:00

I have a fully working core data model, but when I return the data

  • 0

I have a fully working core data model, but when I return the data using a fetch request, it’s in a seemingly random order. What is the best way to sort this data? Is it to use another table in the Core Data model, and ‘query’ the first? Or would it be to pull down the data into an array, and sort it that way?

I’m not too sure how to do either of these, which is the reason that I am asking this question.

  • 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-08T03:51:25+00:00Added an answer on June 8, 2026 at 3:51 am

    Your question is quite general but I’ll try to give you some hints.

    When you use NSFetchRequest class you can specify sort descriptors.

    From Apple doc:

    An array of sort descriptors (instances of NSSortDescriptor) that
    specify how the returned objects should be ordered, for example by
    last name then by first name.

    Here you can find a simple example within Core Data Snippets doc

    NSManagedObjectContext *context = <#Get the context#>;
    
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"<#Entity name#>"
        inManagedObjectContext:context];
    [fetchRequest setEntity:entity];
    
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"<#Sort key#>"
        ascending:YES];
    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
    [fetchRequest setSortDescriptors:sortDescriptors];
    
    NSError *error = nil;
    NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];
    if (fetchedObjects == nil) {
        // Handle the error
    }
    
    // release allocated objects if you don't use ARC
    

    Where

    <#Entity name#> is the name of the entity you want to retrieve, e.g. Manager.

    <#Sort key#> is the name of the key the request will use to order, e.g. name is an attribute of Manager entity.

    So, in my example:

    NSSortDescriptor *sortByName = [[NSSortDescriptor alloc] initWithKey:@"name"
        ascending:YES];
    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortByName, nil];
    [fetchRequest setSortDescriptors:sortDescriptors];
    

    Since setSortDescriptors: sets an array of sort descriptors, you can specify multiple keys against you want to order. e.g. specify to order against name and salary. The sort descriptors are applied in the order in which they appear in the sortDescriptors array. So, in my example, first by name and then by salary.

    So, in my example, it could become

    NSSortDescriptor *sortByName = [[NSSortDescriptor alloc] initWithKey:@"name"
        ascending:YES];
    NSSortDescriptor *sortBySalary = [[NSSortDescriptor alloc] initWithKey:@"salary"
        ascending:YES];
    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortByName, sortBySalary, nil];
    [fetchRequest setSortDescriptors:sortDescriptors];
    

    Hope that helps.

    Edit

    Since fetchedObjects contains NSManagedObject (I suppose you did not change the result type of your request) you need to iterate like the following:

    for(NSManagedObject* currentObj in fetchedObjects) {
    
        NSLog(@"Print the name %@", [currentObj valueForKey:@"name"]);
    }
    

    So, you need to access attributes or relationships through Key Value Coding. To make your life easier you could think to create NSManagedObject subclasses for the entities you created like organising-core-data-for-ios.

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

Sidebar

Related Questions

This is the Core Data object model I am working with (I am a
I have been working with Core Data in an iPad app and I can
I am using devise in a rails 3 application. I have devise fully working
Using: Rails 3.0.3 Friendly_id: 4.0.0.beta11 I have create a fully working development version of
I have a fully working Setup project within Visual Studio 2008 that takes inputs
Ok, I have page one(index), fully functional and working fine as is with all
I have jqGrid 3.5 (full) mostly working. I have it retrieving data with the
I have a very old, very very large, fully working, C program which plays
I have the following code written to make my lazy second CPU core working.
I have been trying for several hours now to get sorl-thumbnail working, but it

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.