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

时间:2011-06-15 04:53:06

标签: iphone ios ipad

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

感谢。

5 个答案:

答案 0 :(得分:9)

到目前为止你尝试了什么?

我还没有这样做,但快速查看文档会让我觉得你应该尝试以下方法:

  1. 致电-contentsOfDirectoryAtURL:includingPropertiesForKeys:options:error:并指定NSURLContentModificationDateKey作为其中一个键。
  2. 您将获得一系列NSURL对象,然后可以使用NSArray方法(如-sortedArrayUsingComparator:进行排序。
  3. 传入比较器块,使用-getResourceValue:forKey:error:查找每个NSURL的修改日期。
  4. 更新:当我写上面的答案时,-getResourceValue:forKey:error:存在于iOS但没有做任何事情。该方法现在从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);
    

答案 1 :(得分:2)

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...

答案 2 :(得分:2)

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:@"%@/%@", documentsDirectory, obj1] error:nil];
                                NSDate *first = [first_properties  objectForKey:NSFileCreationDate];
                                NSDictionary *second_properties = [[NSFileManager defaultManager] attributesOfItemAtPath:[NSString stringWithFormat:@"%@/%@", documentsDirectory, obj2] error:nil];
                                NSDate *second = [second_properties objectForKey:NSFileCreationDate];
                                return [second compare:first];
                            }];

NSLog(@" Date sorted result is %@",filelist_date_sorted);

答案 3 :(得分:0)

NSFileManager以上的类别中:

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] ;
}

答案 4 :(得分:0)

快速方法:

-(void)dateModified
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, 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;
    }
}