यहां दो कदम हैं, उनकी निर्माण तिथियों के साथ फ़ाइलों की सूची प्राप्त करना, और उन्हें सॉर्ट करना।
ताकि इसे बाद में उन्हें सुलझाने के लिए आसान बनाने के लिए, मैं एक वस्तु बनाने के अपने संशोधन दिनांक के साथ पथ धारण करने के लिए:
@interface PathWithModDate : NSObject
@property (strong) NSString *path;
@property (strong) NSDate *modDate;
@end
@implementation PathWithModDate
@end
अब, एक गहरी खोज फ़ाइलों और फ़ोल्डरों (की सूची नहीं प्राप्त करने के लिए), इस का उपयोग करें:
- (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;
}
ध्यान दें कि एक बार मैं PathWithDate ऑब्जेक्ट की श्रृंखला बनाने के लिए, मैं sortUsingComparator
का उपयोग करें (मैं उतरते चुना है) सही क्रम में उन्हें डाल करने के लिए। इसके बजाय निर्माण तिथि का उपयोग करने के लिए, इसके बजाय [attr objectForKey:NSFileCreationDate]
का उपयोग करें।
http://stackoverflow.com/questions/1523793/get-directory-contents-in-date-modified-order यह है? मुझे लगता है कि आप NSFileCreationDate का उपयोग NSFileModificationDate –