Files
panopainter/engine/objc_utils.cpp
2018-04-08 12:28:35 +02:00

37 lines
1.1 KiB
C++

#include "pch.h"
#include "objc_utils.h"
#ifdef __OBJC__
@implementation PathWithModDate
@end
@implementation ObjcUtils
+ (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;
}
@end
#endif