NSSortDescriptor not sorting integers correctly
Asked Answered
I

1

6

I'm trying to sort by date then start time. Start time is minutes from midnight. So if the start time is < 100 it will not sort properly.

- (NSFetchedResultsController *)fetchedResultsController {

    if (fetchedResultsController != nil) {
        return fetchedResultsController;
    }

    /*
     Set up the fetched results controller.
     */
    // Create the fetch request for the entity.
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    // Edit the entity name as appropriate.
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Appointments" inManagedObjectContext:[[DataManager sharedInstance] managedObjectContext]];
    [fetchRequest setEntity:entity];
    [fetchRequest setIncludesPendingChanges:YES];

    // Set the batch size to a suitable number.
    //[fetchRequest setFetchBatchSize:20];

    // Sort using the date / then time property.
    NSSortDescriptor *sortDescriptorDate = [[NSSortDescriptor alloc] initWithKey:@"date" ascending:YES];
    NSSortDescriptor *sortDescriptorTime = [[NSSortDescriptor alloc] initWithKey:@"start_time" ascending:YES];
    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptorDate, sortDescriptorTime, nil];


    [fetchRequest setSortDescriptors:sortDescriptors];

    // Use the sectionIdentifier property to group into sections.
    NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:[[DataManager sharedInstance] managedObjectContext] sectionNameKeyPath:@"date" cacheName:@"List"];
    aFetchedResultsController.delegate = self;
    self.fetchedResultsController = aFetchedResultsController;
    NSLog(@"FetchedController: %@", fetchedResultsController);
    return fetchedResultsController;
}

How could I make this sort integers properly?

Immunology answered 27/3, 2012 at 22:13 Comment(1)
I hope your start_time (in your Core Data object model) is a NSNumber object and not a string.Gyroplane
G
27

If start_time is a string then it will be sorted alphabetically which means that aa is before b which also means that 11 is before 2.

To sort in a more human friendly way use NSString's localizedStandardCompare: as a selector.

[NSSortDescriptor sortDescriptorWithKey:@"start_time" ascending:YES selector:@selector(localizedStandardCompare:)]
Grizelda answered 28/3, 2012 at 3:40 Comment(3)
That was it! thanks. I have it in my core data as a string because it comes in from the API as a string and not a number.Immunology
this is awesome. exactly what i was looking for. I had the problem my string column was sorted this way 1,10,11,2,3,4... above solution solved it.Contessacontest
NSSortDescriptor *descriptor = [NSSortDescriptor sortDescriptorWithKey:@"dateTimeInSec" ascending:YES selector:@selector(localizedStandardCompare:)]; crashed my app. Is the start_time an type NSString attribute or NSNumber? Error was -[__NSCFNumber localizedStandardCompare:]: unrecognized selector sent to instance 0x16dc40c0 with userInfo (null)Pavyer

© 2022 - 2024 — McMap. All rights reserved.