Question

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

Was it helpful?

Solution 3

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];

OTHER TIPS

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:)];

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];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top