0

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", )

A.T
  • 105
  • 3
  • 13
  • Possible duplicate of [How to sort a NSArray alphabetically?](http://stackoverflow.com/questions/1351182/how-to-sort-a-nsarray-alphabetically) – Droppy Jul 22 '16 at 13:33
  • No localizedCaseInsensitiveCompare 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", ) – A.T Jul 22 '16 at 13:37
  • 1
    You want [this one](http://stackoverflow.com/questions/11075644/sort-nsmutablearray-with-strings-that-contain-numbers) then. However you need to make your mind up; do you want it numerical or not? – Droppy Jul 22 '16 at 13:38
  • yes I need numerical. – A.T Jul 22 '16 at 13:40
  • Then your question is incorrect as 2 comes before 12. – Droppy Jul 22 '16 at 13:41
  • No what I write is the out put currently I got..But with this [myArray sortUsingComparator:^NSComparisonResult(NSString *str1, NSString *str2) { return [str1 compare:str2 options:(NSNumericSearch)]; }]; code its working now – A.T Jul 22 '16 at 13:42
  • thank you very much for your help – A.T Jul 22 '16 at 13:43

4 Answers4

3

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
Womble
  • 4,607
  • 2
  • 31
  • 45
2

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"]
Oleg Gordiichuk
  • 15,240
  • 7
  • 60
  • 100
1
[NSSortDescriptor sortDescriptorWithKey:@"self" 
ascending:YEScomparator:^(NSString * string1, NSString * string2){ 
return [string1 compare:string2 options:NSNumericSearch]; 
}];
Bhumit Mehta
  • 16,278
  • 11
  • 50
  • 64
0

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