如何按日期订购数组?

时间:2015-06-16 11:45:35

标签: ios objective-c arrays date

我有一个这种格式的数组:

 <__NSArrayM 0x7fc013d31940>(
        {
            description = "";
            link = "";
            pubDate = "Tue, 16 Jun 2015 07:36:00 +0200";
            title = "";
        },
{
        description = "";
        link = "";
        pubDate = "Mon, 15 Jun 2015 21:18:00 +0200";
        title = "";
    }
        {
            description = "";
            link = "";
            pubDate = "Tue, 16 Jun 2015 11:27:00 +0200";
            title = "";
        },

我想通过pudDate订购。这样做的正确方法是什么?

2 个答案:

答案 0 :(得分:4)

这样的事情:

NSDateFormatter *df = [NSDateFormatter new];
df.dateFormat = @"EEE, d MMM yyyy HH:mm:ss ZZZ";
NSArray *newArr = [arr sortedArrayWithOptions:0 usingComparator:^NSComparisonResult(id obj1, id obj2) {
    NSDate *date1 = [df dateFromString:obj1[@"pubDate"]];
    NSDate *date2 = [df dateFromString:obj2[@"pubDate"]];
    return [date1 compare:date2];
}];

下次使用搜索:explode()

答案 1 :(得分:0)

检查一下,

{{1}}