Reputation: 467
I need to get list of files in a array sorted by created date descending, ie, recent modified file on the top. I checked few built in options with NSFileManager
. Are there any out of box options available?
NSArray *filePathsArray =
[[NSFileManager defaultManager] subpathsOfDirectoryAtPath:documentsDirectory error:nil];
Upvotes: 2
Views: 2024
Reputation: 893
see this , may be it will help
//This is reusable method which takes folder path and returns sorted file list
-(NSArray*)getSortedFilesFromFolder: (NSString*)folderPath
{
NSError *error = nil;
NSArray* filesArray = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:folderPath error:&error];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF EndsWith '.pdf'"];//Take only pdf file
filesArray = [filesArray filteredArrayUsingPredicate:predicate];
// sort by creation date
NSMutableArray* filesAndProperties = [NSMutableArray arrayWithCapacity:[filesArray count]];
for(NSString* file in filesArray) {
if (![file isEqualToString:@".DS_Store"]) {
NSString* filePath = [folderPath stringByAppendingPathComponent:file];
NSDictionary* properties = [[NSFileManager defaultManager]
attributesOfItemAtPath:filePath
error:&error];
NSDate* modDate = [properties objectForKey:NSFileModificationDate];
[filesAndProperties addObject:[NSDictionary dictionaryWithObjectsAndKeys:
file, @"path",
modDate, @"lastModDate",
nil]];
}
}
// Sort using a block - order inverted as we want latest date first
NSArray* sortedFiles = [filesAndProperties sortedArrayUsingComparator:
^(id path1, id path2)
{
// compare
NSComparisonResult comp = [[path1 objectForKey:@"lastModDate"] compare:
[path2 objectForKey:@"lastModDate"]];
// invert ordering
if (comp == NSOrderedDescending) {
comp = NSOrderedAscending;
}
else if(comp == NSOrderedAscending){
comp = NSOrderedDescending;
}
return comp;
}];
return sortedFiles;
}
Upvotes: 3
Reputation: 467
This is reusable method which takes folder path and returns sorted file list
-(NSArray*)getSortedFilesFromFolder: (NSString*)folderPath
{
NSError *error = nil;
NSArray* filesArray = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:folderPath error:&error];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF EndsWith '.pdf'"];//Take only pdf file
filesArray = [filesArray filteredArrayUsingPredicate:predicate];
// sort by creation date
NSMutableArray* filesAndProperties = [NSMutableArray arrayWithCapacity:[filesArray count]];
for(NSString* file in filesArray) {
if (![file isEqualToString:@".DS_Store"]) {
NSString* filePath = [folderPath stringByAppendingPathComponent:file];
NSDictionary* properties = [[NSFileManager defaultManager]
attributesOfItemAtPath:filePath
error:&error];
NSDate* modDate = [properties objectForKey:NSFileModificationDate];
[filesAndProperties addObject:[NSDictionary dictionaryWithObjectsAndKeys:
file, @"path",
modDate, @"lastModDate",
nil]];
}
}
// Sort using a block - order inverted as we want latest date first
NSArray* sortedFiles = [filesAndProperties sortedArrayUsingComparator:
^(id path1, id path2)
{
// compare
NSComparisonResult comp = [[path1 objectForKey:@"lastModDate"] compare:
[path2 objectForKey:@"lastModDate"]];
// invert ordering
if (comp == NSOrderedDescending) {
comp = NSOrderedAscending;
}
else if(comp == NSOrderedAscending){
comp = NSOrderedDescending;
}
return comp;
}];
return sortedFiles;
}
Upvotes: 0