Search code examples
iphoneiossortingcore-datansfetchrequest

Core data sort data by two Descriptor


I have used core data in my ios app. Now i have one table in which there are two columns .

  1. Category

  2. Order (In which there are NSNumber 1, 2 ,5)

I want to fetch data so It will first sort alphabetically by Category name and than by Order Number.

I have used below code :

NSEntityDescription *entity_Maintness = [NSEntityDescription
                                             entityForName:@"Maintness" inManagedObjectContext:__managedObjectContext];
    [fetchRequest_Maintness setEntity:entity_Maintness];


    NSSortDescriptor *sortDescriptor1 = [[NSSortDescriptor alloc] initWithKey:@"type" ascending:YES];

    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"serialNumber" ascending:NO];


    NSArray *sortDescriptors12 = [[NSArray alloc] initWithObjects:sortDescriptor1, sortDescriptor, nil];
    [fetchRequest_Maintness setSortDescriptors:sortDescriptors12];

But the data are sorted only by Category not by Serial Number.

Thanks for Help.


Solution

  • The code above looks ok. Maybe there is an issue elsewhere in your code? Here is some code I quickly generated for you that may serve as starting point??

    #import "MainViewController.h"
    #import "Maintness.h"
    
    @interface MainViewController ()
    @property (weak, nonatomic) IBOutlet UITextField *type;
    @property (weak, nonatomic) IBOutlet UITextField *serialNumber;
    @property (strong, nonatomic) NSManagedObjectContext *context;
    - (IBAction)addButtonPressed:(id)sender;
    - (IBAction)retrieveButtonPressed:(id)sender;
    
    @end
    
    @implementation MainViewController
    
    #pragma mark - lazy instantiation
    - (NSManagedObjectContext *)context{
        if (!_context){
            id appDelegate = (id)[[UIApplication sharedApplication] delegate];
            _context = [appDelegate managedObjectContext];
        }
        return _context;
    }
    
    #pragma mark - core data interactions
    - (IBAction)addButtonPressed:(id)sender {
        NSError *error = nil;
    
        Maintness *newMaintness = nil;
    
        newMaintness = [NSEntityDescription insertNewObjectForEntityForName:@"Maintness" inManagedObjectContext:self.context];
    
        newMaintness.type = self.type.text;
        newMaintness.serialNumber = self.serialNumber.text;
    
        if (![self.context save:&error]) {
            NSLog(@"Oh no - error: %@", [error localizedDescription]);
        } else {
            NSLog(@"It appears the details were added ok");
        }
    
    }
    
    - (IBAction)retrieveButtonPressed:(id)sender {
        NSError *error = nil;
        // Set up fetch
        NSFetchRequest *fetch = [[NSFetchRequest alloc] init];
        // Set up entity
        NSEntityDescription *entity = [NSEntityDescription entityForName:@"Maintness" inManagedObjectContext:self.context];
        [fetch setEntity:entity];
        // Set up sorting
        //   - sorts in order of array
        NSSortDescriptor *primarySortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"type" ascending:YES];
        NSSortDescriptor *secondarySortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"serialNumber" ascending:NO];
        NSArray *sortDescriptors = @[primarySortDescriptor, secondarySortDescriptor];
        [fetch setSortDescriptors:sortDescriptors];
    
        NSArray *results = [self.context executeFetchRequest:fetch error:&error];
    
        for (Maintness *result in results) {
            NSLog(@"type: %@ serial number: %@", result.type, result.serialNumber);
        }
    }
    @end
    

    enter image description here