Get directory contents in date modified order

后端 未结 5 2206
盖世英雄少女心
盖世英雄少女心 2020-12-04 14:19

Is there an method to get the contents of a folder in a particular order? I\'d like an array of file attribute dictionaries (or just file names) ordered by date modified.

5条回答
  •  醉酒成梦
    2020-12-04 15:24

    Simpler...

    NSArray*  filelist_sorted;
    filelist_sorted = [filelist_raw sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
        NSDictionary* first_properties  = [[NSFileManager defaultManager] attributesOfItemAtPath:[NSString stringWithFormat:@"%@/%@", path_thumb, obj1] error:nil];
        NSDate*       first             = [first_properties  objectForKey:NSFileModificationDate];
        NSDictionary* second_properties = [[NSFileManager defaultManager] attributesOfItemAtPath:[NSString stringWithFormat:@"%@/%@", path_thumb, obj2] error:nil];
        NSDate*       second            = [second_properties objectForKey:NSFileModificationDate];
        return [second compare:first];
    }];
    

提交回复
热议问题