I used NSFileManager to retrieve the files in a folder, and I want to sort them by modified date. How to do that ?
Thanks.
How to change the file sort order in iOS. Once you're on the view you want, changing sort order is as easy as tapping the labels at the top of screen. Change the Files app's sort order. You can sort by name, dates, size, kind and tags.
Open File Explorer and navigate to the location where your files are stored. Sort files by Date Modified (Recent files first). Hold Shift key and click on the Name column. This will bring folders at the top with files sorted with Date Modified.
Touch and hold the file or folder, then choose an option: Copy, Duplicate, Move, Delete, Rename, or Compress. To modify multiple files or folders at the same time, tap Select, tap your selections, then tap an option at the bottom of the screen.
Change how files and folders are sorted From an open location or folder, drag down from the center of the screen, then tap Name, Date, Size, Kind, or Tags at the top of the screen.
What have you tried so far?
I haven't done this, but a quick look at the docs makes me think that you should try the following:
-contentsOfDirectoryAtURL:includingPropertiesForKeys:options:error:
and specify NSURLContentModificationDateKey
as one of the keys.-sortedArrayUsingComparator:
.-getResourceValue:forKey:error:
.Update: When I wrote the answer above, -getResourceValue:forKey:error:
existed in iOS but didn't do anything. That method is now functional as of iOS 5. The following code will log an app's resource files followed by a list of corresponding modification dates:
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With