文件按时间先后顺序排序

array包含 文件路径 


- (NSMutableArray *)sortByDateTime:(NSMutableArray *)array
{
    
    NSError *error;
    NSFileManager *fm = [NSFileManager defaultManager];
    NSMutableDictionary *filesAndProperties = [NSMutableDictionary dictionaryWithCapacity:videoArray.count];
    
    for (NSString *name  in array) {
        NSDictionary *properties = [fm attributesOfItemAtPath:name error:&error];
        NSDate *modDate = [properties objectForKey:NSFileCreationDate];
        NSLog(@"data = %@ %@", name,modDate);
        [filesAndProperties setValue:modDate forKey:name];
        
    }
    
    NSArray *ar = [[[filesAndProperties keysSortedByValueUsingSelector:@selector(compare:)] reverseObjectEnumerator] allObjects];
    
    NSMutableArray *ma = [[NSMutableArray alloc] initWithArray:ar];
    
    return ma;
}



你可能感兴趣的:(iOS)