如何在iOS中按修改date对文件进行sorting
我用NSFileManager检索文件夹中的文件,我想按修改datesorting。 怎么做 ?
谢谢。
你试过什么了?
我没有这样做,但快速看文档让我觉得你应该尝试以下几点:
- 调用
-contentsOfDirectoryAtURL:includingPropertiesForKeys:options:error:
并指定NSURLContentModificationDateKey
作为其中一个键。 - 你将得到一个NSURL对象的数组,然后你可以使用NSArray方法(如
-sortedArrayUsingComparator:
进行sorting。 - 传递一个比较器块,使用
-getResourceValue:forKey:error:
来-getResourceValue:forKey:error:
每个NSURL的修改date。
更新:当我写上面的答案时, -getResourceValue:forKey:error:
存在于iOS中,但没有做任何事情。 该方法现在可以在iOS 5中使用。以下代码将logging应用程序的资源文件,然后是相应修改date的列表:
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);
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);
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...
在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] ; }
快速方法:
-(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; } }