A.T
A.T

Reputation: 115

how to sort file from nsdocumentdirectory by their name?

I have some files in nsdocumentdirectory,when I fetched those file its returns the file with random position.I am using this following code:

NSString *downloadDirectory = [Utility bookDownloadFolder];
NSString *bookFolder = [[_selectedBook.zipFile lastPathComponent] stringByDeletingPathExtension];
NSString *bookFolderFinal = [downloadDirectory stringByAppendingPathComponent:bookFolder];
NSMutableArray *retval = [NSMutableArray array];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error = nil;
NSArray *files = [fileManager contentsOfDirectoryAtPath:bookFolderFinal error:&error];

and the output is like this :

files("1.jpg","1.txt","10.jpg","10.txt""11.jpg","11.txt","12.jpg","12.txt","13.jpg","13.txt","2.jpg","2.txt", "3.jpg","3.txt","4.jpg","4.txt","5.jpg","5.txt","6.jpg","6.txt" "7.jpg","7.txt", "__MACOSX" )

But I want the output in ascending order like : files("1.jpg","1.txt","2.jpg","2.txt",)

If I use localizedCaseInsensitiveCompare to sort the array,it is not working in this case,if I use localizedCaseInsensitiveCompare then the output is like this only.( "__MACOSX", "1.jpg", "1.txt", "10.jpg", "10.txt", "11.jpg", "11.txt", "12.jpg", "12.txt", "13.jpg", "13.txt", "2.jpg", "2.txt", "3.jpg", "3.txt", "4.jpg", "4.txt", "5.jpg", )

Upvotes: 0

Views: 1498

Answers (4)

Womble
Womble

Reputation: 5290

Extending and updating Oleg's answer, for Swift 4:

The documentation for contentsOfDirectory(at:) states that the order of the returned URLs is "undefined". I'm not sure many developers expected that, but anyway...

The first job is to get the file URLs:

let maybeURLs = try? FileManager.default.contentsOfDirectory(
        at: rootURL,
        includingPropertiesForKeys: [.isDirectoryKey],
        options: [.skipsSubdirectoryDescendants, .skipsHiddenFiles])

guard let urls = maybeURLs else { return }

Now we have to sort the URLs like the Finder does.

In this case, since we know all the files belong to the same directory, we can compare using only the lastPathComponent(which, happily, is a string.)

You could extend the comparison logic to ignore suffixes (.txt vs .jpg), etc.

let sortedURLs = urls.sorted { a, b in
    return a.lastPathComponent
        .localizedStandardCompare(b.lastPathComponent)
            == ComparisonResult.orderedAscending
}

Print the results:

sortedURLs.forEach { print($0.lastPathComponent) }

1 Foo.txt
10 Bar.txt
100 Baz.txt
200 Boo.txt
210 Moo.txt

Upvotes: 4

Subhojit Das
Subhojit Das

Reputation: 26

NSMutableArray *myArray = [NSMutableArray arrayWithObjects:@"4", @"2", @"7", @"8", nil]; //sorting [myArray sortUsingComparator:^NSComparisonResult(NSString *str1, NSString *str2) { return [str1 compare:str2 options:(NSNumericSearch)]; }]; //logging NSLog(@"%@", myArray);

Upvotes: 0

Subhojit Das
Subhojit Das

Reputation: 26

[NSSortDescriptor sortDescriptorWithKey:@"self" 
ascending:YEScomparator:^(NSString * string1, NSString * string2){ 
return [string1 compare:string2 options:NSNumericSearch]; 
}];

Upvotes: 1

Oleg Gordiichuk
Oleg Gordiichuk

Reputation: 15512

Swift example to solve youre issue:

let testArray = ["1.jpg","1.txt","10.jpg","10.txt","11.jpg","11.txt","12.jpg","12.txt","13.jpg","13.txt","2.jpg","2.txt", "3.jpg","3.txt","4.jpg","4.txt","5.jpg","5.txt","6.jpg","6.txt", "7.jpg","7.txt"]

let sortedArray = testArray.sort({ x, y in
    return x.localizedStandardCompare(y) == NSComparisonResult.OrderedAscending
})


print(sortedArray)
//"["1.jpg", "1.txt", "2.jpg", "2.txt", "3.jpg", "3.txt", "4.jpg", "4.txt", "5.jpg", "5.txt", "6.jpg", "6.txt", "7.jpg", "7.txt", "10.jpg", "10.txt", "11.jpg", "11.txt", "12.jpg", "12.txt", "13.jpg", "13.txt"]

Upvotes: 2

Related Questions