How to sort files by date modified in iOS

I used NSFileManager to retrieve files in a folder, and I want to sort them by date modified. How to do it?

Thank.

+5
source share
5 answers

What have you tried so far?

I did not do this, but a quick look at the documents makes me think that you should try the following:

  • Call -contentsOfDirectoryAtURL:includingPropertiesForKeys:options:error:and specify NSURLContentModificationDateKeyas one of the keys.
  • You will get an array of NSURL objects, which can then be sorted using the NSArray method, for example -sortedArrayUsingComparator:.
  • Go to the comparator block that looks at the change date for each NSURL using -getResourceValue:forKey:error:.

:. , -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);
+8
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
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);
+2
source

In the category above 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] ;
}
0
source

Quick method:

-(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;
    }
}
0
source

All Articles