0

I realize that I am using an older outdated version of Visual Studio, but I am having an issue with getting some files returned with Directory.GetFiles. I wrote a program that is a bible verse game. I am attempting to add another "mode" to the program that allows the users to just use the program as an open bible and read the verses from the book selected. The game part of the program works great and I do not have any issues. I added a combobox to my main form that gets loaded with the verses from the selected book when the user clicks a button. The chapters are in order in the directory (folder) of the respective book [ie: John1.txt, John2.txt, etc]. Using the code below, the chapters are being returned: [John1.txt, John10.txt, John11.txt...]

 Private Sub LoadBook()
    Dim CorrectVersePath As String = Nothing
    
    Select Case My.Settings.Version

        Case "KJV"
            If My.Settings.TestamentName = "NewTestament" Then
                Label2.Text = "New Testament"
                CorrectVersePath = CorrectVersePath & KingJamesPath & "NewTestament\" & My.Settings.BookName & "\"
            ElseIf My.Settings.TestamentName = "OldTestament" Then
                Label2.Text = "Old Testament"
                CorrectVersePath = CorrectVersePath & KingJamesPath & "OldTestament\" & My.Settings.BookName & "\"
            End If

        Case "NIV"
            If My.Settings.TestamentName = "NewTestament" Then
                Label2.Text = "New Testament"
                CorrectVersePath = CorrectVersePath & NewInternationalPath & "NewTestament\" & My.Settings.BookName & "\"
            ElseIf My.Settings.TestamentName = "OldTestament" Then
                Label2.Text = "Old Testament"
                CorrectVersePath = CorrectVersePath & NewInternationalPath & "OldTestament\" & My.Settings.BookName.ToString & "\"
            End If

    End Select

    Dim Chapters = Directory.GetFiles(CorrectVersePath)
    Dim chaptname As Array = Directory.GetFiles(CorrectVersePath)

    For Each cname As String In chaptname
        x += 1
        ComboBox1.Items.Add(IO.Path.GetFileNameWithoutExtension(cname))
    Next
            
End Sub

Is there a way to get those chapters returned in the order they are in the folder? I tried to use the Array.Sort function with the default configuration but it don't work. Yes I read this:

https://learn.microsoft.com/en-us/dotnet/api/system.array.sort?view=net-5.0#System_Array_Sort_System_Array_System_Collections_IComparer_

Smokey
  • 11
  • 2
  • 1
    That's because of the default string ordering method. You instead want the natural sort order. You have different options: 1) Name the files as `ABC0001.txt, ABC0002.txt, ABC0011.txt` etc. 2) `OrderBy()` file name length, `ThenBy()` its value (e.g, `Dim chaptname = Directory.GetFiles(CorrectVersePath).OrderBy(Function(f) f.Length).ThenBy(Function(f) f).ToArray()`) 3) Implement a *Natural Sort Order*. Many options here, start reading these: [Natural Sort Order](https://stackoverflow.com/q/248603/7444103) – Jimi Apr 09 '21 at 04:05
  • Related VB.Net implementations: [How to list collection of files in a directory, in reverse alphanumeric order same as File Explorer...](https://stackoverflow.com/a/52860627/7444103), [How can I natural string sort a datagridview that is databound to a datatable](https://stackoverflow.com/a/61648168/7444103) – Jimi Apr 09 '21 at 04:20
  • Thank you Jimi! Your answer was professional and helpful! Excellent!!! Btw, I'm writing in VB and not C. I am not yet fully familiar with this language. – Smokey Apr 09 '21 at 13:20
  • The links in the second comment point to VB.Net answers. See whether those answer the question, so we can close the issue. – Jimi Apr 09 '21 at 14:30
  • YES! this works GREAT!!! I originally tried typing this in and must have "botched" it up doing so. I copied and pasted this solution into my code and "wa-la!" You are F-in FANTASTIC!!! Kudo's dude!! You can now close this case!!! – Smokey Apr 10 '21 at 00:45

0 Answers0