I have a list of files as follows
10_2017
123_2018
500_2017
20_2019
100_2017
25_2017
32_2018
Which i want to be sorted like
10_2017
25_2017
100_2017
500_2017
32_2018
123_2018
20_2019
I can sort the array by Array.sort(f1,new FileInfoSort)
if I read the years separate but I need it in one sorted array or list
I have tried several threads on here including this method. used f1.Sort(Function(x, y) x.Name.CompareTo(y.Name))
and Array.Sort(f1.toArray, New FileInfoSort)
without any success
I have even tried to separate the files into folders by year and read them separately into lists and combining them which didnt seem to work either
Using the code
Dim d1 As New DirectoryInfo(AppFolder)
Dim f1 As List(Of FileInfo) = d1.GetFiles("*.LBK", SearchOption.TopDirectoryOnly).ToList
'f1.Sort(Function(x, y) x.Name.CompareTo(y.Name))
f1.SortNatural(Function(x) x.Name)
Separating the files into folders by year and then reading it and combining
Dim d1 As New DirectoryInfo(AppFolder.User.data)
Dim d2 As List(Of DirectoryInfo) = New List(Of DirectoryInfo)
'data folders have names like "d_2019"
For Each d As DirectoryInfo In d1.GetDirectories
If d.Name.ToString.Substring(0, 1) = "d" Then d2.Add(d)
Next
Dim f1 As List(Of FileInfo) = New List(Of FileInfo)
For Each Dir As DirectoryInfo In d2
f1.AddRange(Dir.GetFiles("*.LBK", SearchOption.TopDirectoryOnly))
'trying to sort
'f1.Sort(Function(x, y) x.Name.CompareTo(y.Name))
f1.SortNatural(Function(x) x.Name)
Next
Module ListExt
<DllImport("shlwapi.dll", CharSet:=CharSet.Unicode)>
Private Function StrCmpLogicalW(ByVal lhs As String, ByVal rhs As String) As Integer
End Function
<Extension()>
Sub SortNatural(Of T)(ByVal self As List(Of T), ByVal stringSelector As Func(Of T, String))
self.Sort(Function(lhs, rhs) StrCmpLogicalW(stringSelector(lhs), stringSelector(rhs)))
End Sub
<Extension()>
Sub SortNatural(ByVal self As List(Of String))
self.Sort(AddressOf StrCmpLogicalW)
End Sub
End Module
Any method Ive used so far outputs
10_2017
20_2019
25_2017
32_2018
100_2017
123_2018
500_2017
I have no idea how to approach this even. If such a list/array is not possible ideas of how i might structure my files so that i can read in the files in the order i prefer above would be welcome too!