Sorting Array of Objects by Date into TableView

时光总嘲笑我的痴心妄想 提交于 2020-01-07 09:17:10

问题


I used this answer to sort 2 objects by date, and it worked perfectly: Get one NSArray

I now need to sort 3 objects by date, and can't quite modify what I have to get that right.

All of the Articles from the API/RSS feeds will be sorted by date in 1 tableView.

Here's what I tried:

- (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];
    }];   
}

Can you help me sort the 3 dates?


Additional info if needed:

I figured out how to modify this part - Have 3 API/RSS feeds coming in to one 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);
    }];
}

Here's what sorting 2 dates looked like previously:

- (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:


The comparator must always take two values to compare, but it wants to check if those two values are one of three types. Add the following to the public interfaces defined in FeedRSS.h, Data.h and YaRSS.h:

- (NSDate *)sortDate;

In each of the implementations, add a method that returns the right date property to sort on for the class, so, e.g.

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

Same idea for Data.m (return self.created_time), and same for YaRSS.h, return whatever date that object has that you want to sort on. Now your comparator is like this:

- (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];
    }];
}

This works if the array contains only the three kinds of objects you expect. If you always want to sort this way, an even tidier approach is to implement compare: in each of those classes, in each one checking if the param is one of the other two types.



来源:https://stackoverflow.com/questions/26028765/sorting-array-of-objects-by-date-into-tableview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!