IOS中Object-C按照NSDictionary中的某个Key排序的方法

//create_time 降序
NSComparisonResult sort_desc(NSDictionary *firstDict, NSDictionary *secondDict, void *context) {
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
	
    long a = (long) [[dateFormatter dateFromString:[firstDict objectForKey:@"create_time"]] timeIntervalSince1970] * 1000;
    long b = (long) [[dateFormatter dateFromString:[secondDict objectForKey:@"create_time"]] timeIntervalSince1970] * 1000;
    
    if (a >= b)
        return  NSOrderedAscending;
    else
        return  NSOrderedDescending;
}




//调用
NSArray *array = [self.arr copy];
array = [array sortedArrayUsingFunction:sort_desc context:NULL];

  

 

posted @ 2023-01-16 10:47  liskov_design  阅读(134)  评论(0编辑  收藏  举报