Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to sort files by modified date in iOS

Tags:

ios

iphone

ipad

I used NSFileManager to retrieve the files in a folder, and I want to sort them by modified date. How to do that ?

Thanks.

like image 903
user403015 Avatar asked Jun 15 '11 04:06

user403015


People also ask

How do you sort files by Date on iPhone?

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.

How do I sort files by Date modified?

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.

How do I change order of files in iOS?

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.

How do you sort files by Date on IPAD?

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.


1 Answers

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:

  1. Call -contentsOfDirectoryAtURL:includingPropertiesForKeys:options:error: and specify NSURLContentModificationDateKey as one of the keys.
  2. You'll get back an array of NSURL objects which you can then sort using an NSArray method like -sortedArrayUsingComparator:.
  3. Pass in a comparator block that looks up the modification date for each NSURL using -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);
like image 64
Caleb Avatar answered Oct 27 '22 02:10

Caleb