數組中按時間排序


一個字典數組,我先把這個字典數組轉成了模型數組

    //遍歷數據
    [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];

}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM