按日期将对象数组排序到TableView中

时间:2014-09-25 00:47:35

标签: ios objective-c uitableview sorting nsarray

我按日期使用排序2 对象的答案,完美地工作Get one NSArray

我现在需要按日期对3个对象进行排序,并且无法完全修改我的正确所拥有的内容。

API / RSS Feed中的所有文章都将按日期排序为1 tableView

这是我尝试的内容:

- (void)sortCombinedModel {
    // All 3
    [self.combinedModel sortUsingComparator:^NSComparisonResult(id a, id b, id c) {
        NSDate *dateA, *dateB, *dateC;
        dateA = ([a isKindOfClass:[FeedRSS self]])? ((FeedRSS *)a).pubDate : ((Data *)a).created_time : ((YaRSS *)a).pubDate;
        dateB = ([b isKindOfClass:[FeedRSS self]])? ((FeedRSS *)b).pubDate : ((Data *)b).created_time : ((YaRSS *)b).pubDate;
        dateC = ([c isKindOfClass:[FeedRSS self]])? ((FeedRSS *)c).pubDate : ((Data *)c).created_time : ((YaRSS *)c).pubDate;
        return [dateB compare:dateA compare:dateC];
    }];   
}

你能帮我分三个日期吗?


如果需要,请提供其他信息:

我想出了如何修改此部分 - 将3个API / RSS源添加到一个NSMutableArray

- (void)loadMedia {
    self.combinedModel = [NSMutableArray array];
    // Here's the #1
    [self loadOneWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {

        [self.combinedModel addObjectsFromArray:mappingResult.array];

    // Here's the trick.  call API2 here.  Doing so will serialize these two requests
    [self loadTwoWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {

        [self.combinedModel addObjectsFromArray:mappingResult.array];

    // Here's the trick.  call API3 here.  Doing so will serialize these two requests
    [self loadThreeWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {

        [self.combinedModel addObjectsFromArray:mappingResult.array];

        [self sortCombinedModel];

        [self.tableView reloadData];

    } failure:^(RKObjectRequestOperation *operation, NSError *error) {
        NSLog(@"No?: %@", error);
    }];

    } failure:^(RKObjectRequestOperation *operation, NSError *error) {
            NSLog(@"No?: %@", error);
    }];

    } failure:^(RKObjectRequestOperation *operation, NSError *error) {
        NSLog(@"No?: %@", error);
    }];
}

以下是2个日期的排序方式:

- (void)sortCombinedModel {
    [self.combinedModel sortUsingComparator:^NSComparisonResult(id a, id b) {
        NSDate *dateA, *dateB;
        dateA = ([a isKindOfClass:[Feed self]])? ((Feed *)a).published : ((Data *)a).created_time;
        dateB = ([b isKindOfClass:[Feed self]])? ((Feed *)b).published : ((Data *)b).created_time;
        return [dateA compare:dateB];
    }];
}

1 个答案:

答案 0 :(得分:1)

比较器必须始终采用两个值进行比较,但是它想检查这两个值是否是三种类型之一。将以下内容添加到FeedRSS.h,Data.h和YaRSS.h中定义的公共接口:

- (NSDate *)sortDate;

在每个实现中,添加一个方法,该方法返回正确的日期属性以对类进行排序,例如,

// FeedRSS.m
- (NSDate *)sortDate {
    return self.pubDate;
}

对于Data.m(返回self.created_time)也是如此,对于YaRSS.h也是如此,返回您想要排序的对象的任何日期。现在你的比较器是这样的:

- (void)sortCombinedModel {
    [self.combinedModel sortUsingComparator:^NSComparisonResult(id a, id b) {
        NSDate *dateA = nil, *dateB = nil;
        if ([a isKindOfClass:[Feed self]]) { dateA = ((Feed *)a).sortDate; }
        else if ([a isKindOfClass:[Data self]]) { dateA = ((Data *)a).sortDate; }
        else if ([a isKindOfClass:[YaRSS self]]) { dateA = ((YaRSS *)a).sortDate; }

        if ([b isKindOfClass:[Feed self]]) { dateB = ((Feed *)b).sortDate; }
        else if ([b isKindOfClass:[Data self]]) { dateB = ((Data *)b).sortDate; }
        else if ([b isKindOfClass:[YaRSS self]]) { dateB = ((YaRSS *)b).sortDate; }

        return [dateA compare:dateB];
    }];
}

如果数组只包含您期望的三种对象,则此方法有效。如果你总是想用这种方式排序,一个更整洁的方法是实现比较:在每个类中,检查每个类是否是另外两种类型之一。