数组中按时间排序

一个字典数组,我先把这个字典数组转成了模型数组

    //遍历数据
    [messKeyArray enumerateObjectsUsingBlock:^(NSDictionary *  _Nonnull messKeyDictionary, NSUInteger idx, BOOL * _Nonnull stop) {
        
#pragma mark --保存到数据库后字典转模型
        DYMessageTableModel *model = [DYMessageTableModel messageTableModelWithDic:messKeyDictionary];
        
        [nmArray addObject:model];
    }];

 然后调用数组按时间排序的方法

 NSArray *sortedArray = [nmArray sortedArrayUsingSelector:@selector(compareByDate:)];

 方法如下

//按时间排序
-(NSComparisonResult)compareByDate:(DYMessageTableModel *)model
{
    NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init];
    [inputFormatter setDateFormat:@"yyyyMMddHHmm"];
    NSDate* firstDate = [inputFormatter dateFromString:[NSString stringWithFormat:@"%@",self.timeString]];
    NSDate* secondeDate = [inputFormatter dateFromString:[NSString stringWithFormat:@"%@",model.timeString]];
    
    return [secondeDate compare:firstDate];

}

 

posted @ 2016-12-14 10:22  小眼奇遇记  阅读(2984)  评论(0编辑  收藏  举报