首先按照一对多关系对一组自定义子对象进行排序

时间:2015-02-03 12:24:11

标签: objective-c nsarray nssortdescriptor

我正在尝试对数组进行排序,以使最新的数组位于顶部。

我遇到的问题是我正在对一对多的关系进行排序,而使用比较似乎并没有对它进行排序。

该阵列是已完成的航班列表。

NSFetchRequest *fr = [[NSFetchRequest alloc] initWithEntityName:[self entityName]];
    fr.predicate = [NSPredicate predicateWithFormat:@"(acceptedDate != nil || booked == %@ || ANY legs.flight != nil) && ANY legs.departureDate <= %@", @YES, [NSDate date]];

    NSArray *results = [context executeFetchRequest:fr error:nil];

    //Check BOTH legs have completed        
    NSDate *now = [NSDate date];
    NSMutableArray *filtered = [NSMutableArray array];

    for (Quote *quote in results) {
        if ([quote.legs count] > 1) {
            BOOL completed = YES;
            for (QuoteLeg *leg in quote.legs) {
                if ([leg.departureDate compare:now] == NSOrderedDescending) {
                    completed = NO;
                }
            }                
            if (completed) {
                [filtered addObject:quote];
            }
        }
        else {
            [filtered addObject:quote];
        }
    }

    // Try to sort the array
    [filtered sortedArrayWithOptions:NSSortStable usingComparator:^NSComparisonResult(Quote *quote1, Quote *quote2){
       QuoteLeg *leg1 = [[quote1.legs allObjects] firstObject];
       QuoteLeg *leg2 = [[quote2.legs allObjects] firstObject];

       return [leg1.departureDate compare:leg2.departureDate];
    }];

    // Sort filtered by date
    for (Quote *q in filtered) {
        QuoteLeg *leg = [[q.legs allObjects] firstObject];
        NSLog(@"date = %@", leg.departureDate);
    }

这总是输出

date = 2015-01-20 11:00:00 +0000
date = 2015-01-23 12:00:00 +0000
date = 2015-01-29 12:00:00 +0000
date = 2015-01-30 10:40:00 +0000
date = 2015-01-30 10:40:00 +0000
date = 2015-01-29 09:00:00 +0000
date = 2015-01-26 10:00:00 +0000

我需要确保日期是最新的。

我还能做些什么吗?

2 个答案:

答案 0 :(得分:1)

- (NSArray *)sortedArrayWithOptions:(NSSortOptions)opts usingComparator:(NSComparator)cmptr

方法会将结果作为NSArray返回,但不会对NSMutableArray进行排序。试着这样做

filtered = [filtered sortedArrayWithOptions:NSSortStable usingComparator:^NSComparisonResult(Quote *quote1, Quote *quote2){
       QuoteLeg *leg1 = [[quote1.legs allObjects] firstObject];
       QuoteLeg *leg2 = [[quote2.legs allObjects] firstObject];

        return [leg1.departureDate compare:leg2.departureDate];
            }];

答案 1 :(得分:0)

我想我有一个答案。我尝试在没有任何密钥的情况下在其周围包裹NSSortDescriptor,结果似乎按照我要求的顺序;

        // Filtered is a NSMutableArray
        NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:nil ascending:NO
                                                                      comparator:^NSComparisonResult(VICQuote *quote1, VICQuote *quote2){
                                                                          VICQuoteLeg *leg1 = [[quote1.legs allObjects] firstObject];
                                                                          VICQuoteLeg *leg2 = [[quote2.legs allObjects] firstObject];

                                                                        return [leg1.departureDate compare:leg2.departureDate];
                                                                      }];
        [filtered sortUsingDescriptors:@[sortDescriptor]];

        NSArray *finalResults = [NSArray arrayWithArray:filtered];

        // Output them
        for (VICQuote *q in finalResults) {
            VICQuoteLeg *leg = [[q.legs allObjects] firstObject];
            NSLog(@"date = %@", leg.departureDate);
        }

结果:

date = 2015-01-31 10:35:00 +0000
date = 2015-01-30 09:40:00 +0000
date = 2015-01-29 12:00:00 +0000
date = 2015-01-29 09:00:00 +0000
date = 2015-01-28 11:00:00 +0000
date = 2015-01-23 12:00:00 +0000
date = 2015-01-20 11:00:00 +0000