Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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];
    }];
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
157 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...