Apple
Apple

Reputation: 854

Sorting NSDocumentDirectory by name

I want to sort all NSDocumentDirectory by fileName but folders should be first by name and then all files are sorted by name.

In my Document Directory, i have lots of files and folders also.

i have the array like,

_filePathsArray =[[NSFileManager defaultManager] contentsOfDirectoryAtPath:_selectedPath error:Nil];

Thanks in advance

Upvotes: 2

Views: 736

Answers (3)

Apple
Apple

Reputation: 854

Below code helps me... And thanks friends for your reply.

NSArray *fileArray = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:_selectedPath error:Nil];

_filePathsArray = [[NSMutableArray alloc]init];
NSMutableArray *fileArray1 = [[NSMutableArray alloc]init];
for(int i=0;i<[fileArray count];i++)
{
    if ([[[fileArray objectAtIndex:i] pathExtension] isEqualToString:@""])
    {
        [_filePathsArray addObject:[fileArray objectAtIndex:i]];
    }
    else
    {
        [fileArray1 addObject:[fileArray objectAtIndex:i]];
    }

}

[_filePathsArray addObjectsFromArray:fileArray1];

Upvotes: 1

iPhone 7
iPhone 7

Reputation: 1741

Use this.

 [yourArray sortUsingSelector:@selector(localizedCaseInsensitiveCompare:)];

For NSArray try this.

 NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:@"interest"  ascending:YES];
[stories sortedArrayUsingDescriptors:[NSArray arrayWithObjects:descriptor,nil]];
recent = [stories copy];

Upvotes: 1

Bhavin_m
Bhavin_m

Reputation: 2784

You can sort array by alphabetically by following way. i am not sure about folders comes first.

[myArray sortUsingSelector:@selector(caseInsensitiveCompare:)];

OR

sortedArray = [anArray sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];

Upvotes: 6

Related Questions