【问题标题】:How to sort NSDate by date descending, but with time ascending within that date?如何按日期降序对 NSDate 进行排序,但在该日期内时间升序?
【发布时间】:2012-07-19 07:30:33
【问题描述】:

我使用 NSFetchedResultsController 和 NSSortDescriptor 成功排序到一个表格中,每个日期都有一个部分,因此今天的日期首先出现(日期降序)。

但是,在该部分中,我希望按升序对时间进行排序。

这是我当前的代码,没有按时间排序:

//Set up the fetched results controller.
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:kEntityHistory inManagedObjectContext:global.managedObjectContext];
fetchRequest.entity = entity;

// Sort using the timeStamp property..
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"date" ascending:NO];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];

fetchRequest.sortDescriptors = sortDescriptors;

//The following is from:
//http://stackoverflow.com/questions/7047943/efficient-way-to-update-table-section-headers-using-core-data-entities

NSString *sortPath = @"date.dayDate";

self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:global.managedObjectContext sectionNameKeyPath:sortPath cacheName:nil];

fetchedResultsController.delegate = self;

请问如何更改代码以在此日期部分中按时间升序添加排序?

【问题讨论】:

    标签: ios nsdate


    【解决方案1】:

    我认为您需要一个特殊的比较器。使用initWithKey:ascending:comparator: 初始化您的排序描述符并作为比较器传递:

    ^(id obj1, id obj2) {
        NSDateComponents *components1 = [[NSCalendar currentCalendar] components:NSHourCalendarUnit|NSDayCalendarUnit fromDate:obj1];
        NSInteger day1 = [components1 day];
        NSInteger hour1 = [components1 hour];
    
        NSDateComponents *components2 = [[NSCalendar currentCalendar] components:NSHourCalendarUnit|NSDayCalendarUnit fromDate:obj2];
        NSInteger day2 = [components2 day];
        NSInteger hour2 = [components2 hour];
    
        NSComparisonResult res;
        if (day1>day2) {
            res = NSOrderedAscending;
        } else if (day1<day2) {
            res = NSOrderedDescending;
        } else {
            if (hour1>hour2) {
                res = NSOrderedDescending;
            } else {
                res = NSOrderedAscending;
            }
        }
        return res;
    }
    

    这应该让您了解如何实现这一点,您将需要添加分钟和秒组件,并处理小时、分钟和秒相等的情况。

    【讨论】:

    • 谢谢 - 有道理。从那以后我发现(我认为) NSFetchedResultsController 不支持自定义排序描述符,所以明天我将尝试在获取的数组上使用您的代码,然后研究如何将其放回部分。
    • 我很确定这应该像我描述的那样有效,所以如果可以的话,请先尝试一下。 NSFetchedResultsController 与排序过程没有太大关系,都发生在 NSFetchRequest 中,具体在这里:fetchRequest.sortDescriptors = sortDescriptors;
    • iOS 似乎不支持它:'NSInvalidArgumentException', reason: 'unsupported NSSortDescriptor (comparator blocks are not supported)'。 @iawicko 你的答案应该在 Mac OS X 上使用吗?
    • 不,这个类是基础框架的一部分,所以它应该可以工作,但是这个特定的方法是新的,所以你需要最新的 OSX Available in OS X v10.6 and later.
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-22
    • 2013-01-10
    • 1970-01-01
    • 2022-01-09
    • 1970-01-01
    相关资源
    最近更新 更多