如何在iOS中按修改日期对文件进行排序

问题描述

|| 我使用NSFileManager检索文件夹中的文件,我想按修改日期对它们进行排序。怎么做 ? 谢谢。     

解决方法

        你都尝试了些什么? 我还没有这样做,但是快速浏览一下文档使我认为您应该尝试以下操作: 呼叫“ 0”并指定“ 1”作为键之一。 您将获得一个NSURL对象数组,然后可以使用诸如2的NSArray方法对它们进行排序。 传递一个比较器块,该块使用
-getResourceValue:forKey:error:
查找每个NSURL的修改日期。 更新:当我写下上面的答案时,iOS中存在
-getResourceValue:forKey:error:
,但没有执行任何操作。该方法现已从iOS 5开始运行。以下代码将记录应用程序的资源文件,然后记录相应的修改日期:
NSFileManager *manager = [NSFileManager defaultManager];
NSArray *files = [manager contentsOfDirectoryAtURL:[[NSBundle mainBundle] resourceURL]
                        includingPropertiesForKeys:[NSArray arrayWithObject:NSURLContentModificationDateKey]
                                           options:nil
                                             error:nil];
NSMutableArray *dates = [NSMutableArray array];
for (NSURL *f in files) {
    NSDate *d = nil;
    if ([f getResourceValue:&d forKey:NSURLContentModificationDateKey error:nil]) {
        [dates addObject:d];
    }
}
NSLog(@\"Files: %@\",files);
NSLog(@\"Dates: %@\",dates);
    ,        
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSFileManager *manager = [NSFileManager defaultManager];
    NSArray *imageFilenames = [manager contentsOfDirectoryAtPath:documentsDirectory error:nil];
    NSMutableArray *originalImage = [[NSMutableArray alloc]init];

    for (int i = 1; i < [imageFilenames count]; i++)
    {
        NSString *imageName = [NSString stringWithFormat:@\"%@/%@\",documentsDirectory,[imageFilenames objectAtIndex:i] ];

    }

    //---------sorting image by date modified
    NSArray*  filelist_date_sorted;

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

NSLog(@\" Date sorted result is %@\",filelist_date_sorted);
    ,        
static static NSInteger contentsOfDirSort(NSString *left,NSString *right,void *ptr) {

    (void)ptr;

    struct stat finfo_l,r_finfo_r;

    if(-1 == stat([left UTF8String],&finfo_l))
        return NSOrderedSame;

    if(-1 == stat([right UTF8String],&finfo_r))
        return NSOrderedSame;

    if(finfo_l.st_mtime < finfo_r.st_mtime)
        return NSOrderedAscending;

    if(finfo_l.st_mtime > finfo_r.st_mtime)
        return NSOrderedDescending;

    return NSOrderedSame;
}
现在,稍后在您的代码中。
NSMutableArray *mary = [NSMutableArray arrayWithArray:filePathsArray];

[mary sortUsingFunction:contentsOfDirSort context:nil];

// Use mary...
    ,        在ѭ9以上类别中:
static  NSInteger contentsOfDirSort(NSURL *leftURL,NSURL *rightURL,void *ptr)
{
    (void)ptr;

    NSDate * dateLeft ;
    [leftURL getResourceValue:&dateLeft
                       forKey:NSURLContentModificationDateKey
                        error:nil] ;

    NSDate * dateRight ;
    [leftURL getResourceValue:&dateRight
                       forKey:NSURLContentModificationDateKey
                        error:nil] ;

    return [dateLeft compare:dateRight];
}




- (NSArray *)contentsOrderedByDateOfDirectoryAtPath:(NSURL *)URLOfFolder ;
{
    NSArray *files = [self contentsOfDirectoryAtURL:URLOfFolder
                         includingPropertiesForKeys:[NSArray arrayWithObject:NSURLContentModificationDateKey]
                                            options:0
                                              error:nil];

    return [files sortedArrayUsingFunction:contentsOfDirSort
                                   context:nil] ;
}
    ,        快速方法:
-(void)dateModified
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];

    NSFileManager *manager = [NSFileManager defaultManager];
    NSArray *fileList = [manager contentsOfDirectoryAtPath:documentsDirectory error:nil];

    //--- Listing file by name sort
    NSLog(@\"\\n File list %@\",fileList);




    int num;
    //-- Listing file name with modified dated
    for (NSString *s in fileList)
    {
        NSString *filestring = [documentsDirectory stringByAppendingFormat:@\"/%@\",s];

        NSDictionary *filePathsArray1 = [[NSFileManager defaultManager] attributesOfItemAtPath:filestring error:nil];
        NSString *modifiedDate = [filePathsArray1 objectForKey:NSFileModificationDate];
        NSLog(@\"\\n Modified Day : %@\",modifiedDate);

        num=num+1;
    }
}
    

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...