按日期将对象数组排序到TableView中 [英] Sorting Array of Objects by Date into TableView

查看:100
本文介绍了按日期将对象数组排序到TableView中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用此答案按日期对对象进行了 2个排序,并且它工作完美:

以下是排序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];
    }];
}

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

- (NSDate *)sortDate;

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

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

如果数组仅包含您期望的三种对象,则此方法有效.如果您始终希望以这种方式进行排序,那么一种比较整洁的方法是实现compare:在每个这些类中,在每个类中检查参数是否为其他两种类型之一.

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

解决方案

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.

这篇关于按日期将对象数组排序到TableView中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆