0

Hello guys I am trying to Get files from a folder that contains Episodes of a show that has more than 100 episodes. But When I retrieve the files with d.GetFiles("*.*", SearchOption.TopDirectoryOnly) It is giving me all the files but It is getting episode 100 right after 1 and 2 after 100+. How can I get the files in correct order?

DFlow
  • 11
  • 2
  • 1
    There is a Windows API function that can be used to sort the array returned by `GetFiles`. See [this question](https://stackoverflow.com/questions/3099581/sorting-an-array-of-folder-names-like-windows-explorer-numerically-and-alphabet) for the solution. – jmcilhinney Jun 10 '18 at 02:56
  • Thank you but, I have created the class and implemented the function as Array.Sort(myImageFileInfos, New MyComparer) but it is throwing me Unable to cast object of type MyComparer to type 'System.Collections.iComparer'. What to do? sorry – DFlow Jun 10 '18 at 03:14

1 Answers1

1

Got it to working. The class Implements IComparer(Of String) accepts List of string. Because of this I just had to convert the GetFiles to a List(Of String) and then sort the List as so:

The_Files = Directory.GetFiles(Folder_Browser_Dialog.SelectedPath & "\" & i, "*.*").ToList
The_Files.Sort(New MyComparer)

This worked PERFECTLY Thank you soo much @jmcilhinney

DFlow
  • 11
  • 2
  • 1
    You don't need a `List`. As the thread I linked to demonstrates, the `Array.Sort` method accepts an `IComparer(Of String)` too. Get rid of the `ToList` call, make `The_Files` a `String` array and call `Array.Sort(The_Files, New MyComparer)`. – jmcilhinney Jun 10 '18 at 03:46
  • @jmcilhinney : [According to the OP](https://stackoverflow.com/questions/50780172/sorting-a-getfiles-in-alphabetic-and-numeric-order#comment88567227_50780172) that didn't work for some reason... – Visual Vincent Jun 10 '18 at 11:37
  • 1
    @VisualVincent, the OP said that their class could not be cast as type `IComparer` which means that they didn't implement that interface in that class. This answer indicates that they did then implement that interface so that solves the problem they were having. The additional use of a `List` instead of an array was not required. – jmcilhinney Jun 10 '18 at 11:48