NSSortDescriptor没有正确排序整数 [英] NSSortDescriptor not sorting integers correctly

查看:108
本文介绍了NSSortDescriptor没有正确排序整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试按日期排序然后开始时间。开始时间是从午夜开始的几分钟。因此,如果开始时间是< 100它将无法正确排序。

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?

推荐答案

如果start_time是一个字符串,那么它将按字母顺序排序,这意味着 aa 在<$之前c $ c> b 这也意味着 11 2 之前。

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.

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

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

[NSSortDescriptor sortDescriptorWithKey:@"start_time" ascending:YES selector:@selector(localizedStandardCompare:)]

这篇关于NSSortDescriptor没有正确排序整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆