Fire Fist
Fire Fist

Reputation: 7050

How to sort file from document directory by Created Date in iOS?

I have a NSMutableArray that retrieved file from document directory in iOS.

i retrieved files from document directory with following codes.

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSError * error;
    self.array = [[NSMutableArray alloc] init];

    self.array = (NSMutableArray *)[[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsDirectory error:&error];

And then i show the lists of file in UITableView. However it show with Alphabetically.

I want to show it sorted by Created Date of file.

Edit

Here is i write in ViewWillAppear.

- (void)viewWillAppear:(BOOL)animated
{   
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSError * error;
    self.array = [[NSMutableArray alloc] init];

    self.array = (NSMutableArray *)[[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsDirectory error:&error];

    [self loadData];  
 }

- (void) loadData
{
    self.sortedFileList = [self.array mutableCopy];
    [self.sortedFileList sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {
        NSDate* d1 = [[NSFileManager defaultManager] attributesOfFileSystemForPath:obj1 error:nil][@"fileCreationDate"];
        NSDate* d2 = [[NSFileManager defaultManager] attributesOfFileSystemForPath:obj2 error:nil][@"fileCreationDate"];
        return [d1 compare:d2];
    }];

}

And It not sort by date.

How can i do that?

Thanks for any suggestions and help.

Upvotes: 1

Views: 3319

Answers (3)

Bhavin
Bhavin

Reputation: 27225

One thing you can do is to store FileNames as a Key and CreationDate as a Value in some NSDictionary and then you can use ...

myArray = [myDict keysSortedByValueUsingComparator: ^NSComparisonResult(id obj1, id obj2){
                  return [[obj2 date] compare:[obj1 date]];
          }];

Upvotes: 3

Krumelur
Krumelur

Reputation: 32497

One way is to use the sortUsingComparator: method on the mutable array, e.g. (pseudo code):

@property (nonatomic,retain) NSMutableArray* sortedFileList;
....

// Make sure to call this method *once* (i.e. from your VC when loading view).
- (void) loadData {
    self.sortedFileList = [self.array mutableCopy];
    [a sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {
        NSDate* d1 = [[NSFileManager defaultManager] attributesOfItemAtPath:obj1 error:nil][@"fileCreationDate"];
        NSDate* d2 = [[NSFileManager defaultManager] attributesOfItemAtPath:obj2 error:nil][@"fileCreationDate"];
        return [d1 compare:d2];            
    }];

}

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section     {
    return self.sortedFileList.count;
}

- (UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell* cell = // dequeue cell or create one



    cell.textLabel.text = [[[NSString stringWithFormat:@"%@",[self.sortedFileList objectAtIndex:indexPath.row]] lastPathComponent] stringByDeletingPathExtension];
}

Upvotes: 2

Wain
Wain

Reputation: 119031

You need to us NSFileManager get the attributes (attributesOfItemAtPath:error:) for each file. This returns a dictionary for the file and you can get the creation date with fileCreationDate. Then you can perform your sorting.

Upvotes: 3

Related Questions