NSSortDescriptor没有正确排序整数

Bot*_*Bot 6 objective-c nssortdescriptor nsfetchedresultscontroller ios5

我想按日期排序,然后开始时间.开始时间是从午夜开始的几分钟.因此,如果开始时间<100,则无法正确排序.

- (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;
}
Run Code Online (Sandbox Code Playgroud)

我怎么能正确地使这种整数?

Nat*_*ger 27

如果start_time是一个字符串,那么它将按字母顺序排序,这意味着aab此之前也意味着它11在之前2.

要以更人性化的方式排序,请使用NSString's localizedStandardCompare:作为选择器.

[NSSortDescriptor sortDescriptorWithKey:@"start_time" ascending:YES selector:@selector(localizedStandardCompare:)]
Run Code Online (Sandbox Code Playgroud)

  • 这太棒了.正是我在寻找的东西.我有问题我的字符串列以这种方式排序1,10,11,2,3,4 ......上面的解决方案解决了它. (2认同)