Sub ListFiles(strStartFolder As String, _
arrStrFilter() As String) As List
Dim i As Int
Dim n As Int
Dim iUBFilters As Int
Dim lstFiles As List
Dim lstFiles2 As List
Dim lstFiles3 As List
Dim lstExtensions As List
Dim arrIndexes() As Int
Dim strFile As String
Dim strExtension As String
iUBFilters = arrStrFilter.Length - 1
lstFiles.Initialize
lstFiles = File.ListFiles(strStartFolder)
If lstFiles.IsInitialized = False Then
Return lstFiles3 'return uninitialized list
End If
lstFiles2.Initialize
lstFiles3.Initialize
lstExtensions.Initialize
If arrStrFilter.Length > 0 Then
For i = 0 To lstFiles.Size - 1
strFile = lstFiles.Get(i)
If File.IsDirectory(strStartFolder, strFile) = False Then
strExtension = strFile.SubString(strFile.LastIndexOf(".") + 1)
For n = 0 To iUBFilters
If strExtension = arrStrFilter(n) Then
lstFiles2.Add(strFile)
lstExtensions.Add(strExtension)
Exit
End If
Next
End If
Next
Else
For i = 0 To lstFiles.Size - 1
strFile = lstFiles.Get(i)
If File.IsDirectory(strStartFolder, strFile) = False Then
strExtension = strFile.SubString(strFile.LastIndexOf(".") + 1)
lstFiles2.Add(strFile)
lstExtensions.Add(strExtension)
End If
Next
End If
arrIndexes = cSorting.SortOMStringListIDX(lstExtensions, True, False, -1, -1)
For i = 0 To arrIndexes.Length - 1
lstFiles3.Add(lstFiles2.Get(arrIndexes(i)))
Next
Return lstFiles3
End Sub
Sub SortOMStringListIDX(oList As List, bAscending As Boolean, bCaseInsensitive As Boolean, iStart As Int, iEnd As Int) As Int()
Dim i As Long
Dim c As Long
Dim n As Long
Dim B4XOM As B4XOrderedMap
Dim arrString(oList.Size) As String
If iStart = -1 Then
iStart = 0
End If
If iEnd = -1 Then
iEnd = oList.Size - 1
End If
For i = iStart To iEnd
arrString(i) = oList.Get(i)
Next
If bCaseInsensitive Then
For i = iStart To iEnd
arrString(i) = arrString(i).ToLowerCase
Next
End If
If Check1DStringAllSame(arrString, False, iStart, iEnd) Then
Dim arrIndex2(1) As Int
arrIndex2(0) = -1 'telling the calling Sub that all items were same, so need for any sort
Return arrIndex2
End If
B4XOM.Initialize
For i = iStart To iEnd
Dim lstIndexes As List
If B4XOM.ContainsKey(arrString(i)) Then
lstIndexes = B4XOM.Get(arrString(i))
Else
lstIndexes.Initialize
End If
lstIndexes.Add(i)
B4XOM.Put(arrString(i), lstIndexes)
Next
B4XOM.Keys.Sort(bAscending)
Dim arrIndex((iEnd - iStart) + 1) As Int
For Each oValue As Object In B4XOM.Values
lstIndexes = oValue
For c = 0 To lstIndexes.Size - 1
arrIndex(n) = lstIndexes.Get(c)
n = n + 1
Next
Next
Return arrIndex
End Sub
Sub Check1DStringAllSame(arrstring() As String, bCaseInsensitive As Boolean, iStart As Int, iEnd As Int) As Boolean
Dim i As Int
Dim str As String
If arrstring.Length = 1 Then
Return True
End If
If iStart = -1 Then
iStart = 1
End If
If iEnd = -1 Then
iEnd = arrstring.Length - 1
End If
If bCaseInsensitive Then
str = arrstring(0).ToLowerCase
For i = iStart To iEnd
If arrstring(i).ToLowerCase.CompareTo(str) <> 0 Then
Return False
End If
Next
Else
str = arrstring(0)
For i = iStart To iEnd
If arrstring(i).CompareTo(str) <> 0 Then
Return False
End If
Next
End If
Return True
End Sub