按创建日期排序文件 - iOS [英] Sort files by creation date - iOS

查看:500
本文介绍了按创建日期排序文件 - iOS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取i目录中的所有文件,并根据创建日期或修改日期对它们进行排序。有很多例子,但是我不能让它们中的任何一个工作。



任何人都有一个很好的例子如何从目录中获取一个文件数组按日期排序?

解决方案

这里有两个步骤,获取包含创建日期的文件列表,然后对它们进行排序。 / p>

为了便于以后对它们进行排序,我创建了一个对象来保存带有修改日期的路径:

  @interface PathWithModDate:NSObject 
@property(strong)NSString * path;
@property(强)NSDate * modDate;
@end

@implementation PathWithModDate
@end

现在,要获取文件和文件夹列表(不是深度搜索),请使用:

   - (NSArray * )getFilesAtPathSortedByModificationDate:(NSString *)folderPath {
NSArray * allPaths = [NSFileManager.defaultManager contentsOfDirectoryAtPath:folderPath error:nil];

NSMutableArray * sortedPaths = [NSMutableArray new];
for(NSString * allPaths中的路径){
NSString * fullPath = [folderPath stringByAppendingPathComponent:path];

NSDictionary * attr = [NSFileManager.defaultManager attributesOfItemAtPath:fullPath error:nil];
NSDate * modDate = [attr objectForKey:NSFileModificationDate];

PathWithModDate * pathWithDate = [[PathWithModDate alloc] init];
pathWithDate.path = fullPath;
pathWithDate.modDate = modDate;
[sortedPaths addObject:pathWithDate];
}

[sortedPaths sortUsingComparator:^(PathWithModDate * path1,PathWithModDate * path2){
//降序(最近修改的第一个)
return [path2.modDate比较:path1.modDate];
}];

返回sortedPaths;
}

请注意,一旦我创建了PathWithDate对象数组,我就会使用 sortUsingComparator 将它们按正确顺序排列(我选择降序)。为了使用创建日期,请改用 [attr objectForKey:NSFileCreationDate]


I´m trying to get all the files in i directory and sort them according to creation date or modify date. There are plenty of examples out there but I can´t get no one of them to work.

Anyone have a good example how to get an array of files from a directory sorted by date?

解决方案

There are two steps here, getting the list of files with their creation dates, and sorting them.

In order to make it easy to sort them later, I create an object to hold the path with its modification date:

@interface PathWithModDate : NSObject
@property (strong) NSString *path;
@property (strong) NSDate *modDate;
@end

@implementation PathWithModDate
@end

Now, to get the list of files and folders (not a deep search), use this:

- (NSArray*)getFilesAtPathSortedByModificationDate:(NSString*)folderPath {
    NSArray *allPaths = [NSFileManager.defaultManager contentsOfDirectoryAtPath:folderPath error:nil];

    NSMutableArray *sortedPaths = [NSMutableArray new];
    for (NSString *path in allPaths) {
        NSString *fullPath = [folderPath stringByAppendingPathComponent:path];

        NSDictionary *attr = [NSFileManager.defaultManager attributesOfItemAtPath:fullPath error:nil];
        NSDate *modDate = [attr objectForKey:NSFileModificationDate];

        PathWithModDate *pathWithDate = [[PathWithModDate alloc] init];
        pathWithDate.path = fullPath;
        pathWithDate.modDate = modDate;
        [sortedPaths addObject:pathWithDate];
    }

    [sortedPaths sortUsingComparator:^(PathWithModDate *path1, PathWithModDate *path2) {
        // Descending (most recently modified first)
        return [path2.modDate compare:path1.modDate];
    }];

    return sortedPaths;
}

Note that once I create an array of PathWithDate objects, I use sortUsingComparator to put them in the right order (I chose descending). In order to use creation date instead, use [attr objectForKey:NSFileCreationDate] instead.

这篇关于按创建日期排序文件 - iOS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆