I want to sort an array containing only date values.
I want the latest date in the upper bound of the array:
Function vba_sort_array_a_to_z(ByRef myArray)
Dim i As Integer
Dim j As Integer
Dim Temp As String
'sorting array from A to Z
For i = LBound(myArray) To UBound(myArray)
For j = i + 1 To UBound(myArray)
If UCase(myArray(i)) > UCase(myArray(j)) Then
Temp = myArray(j)
myArray(j) = myArray(i)
myArray(i) = Temp
End If
Next j
Next i
End Function
Expecting "12.04.2022" to be sorted to the upper bound of the array:
Sub sortThisArray()
Dim vDate() As Date: ReDim vDate(0 To 2)
vDate(0) = "25.03.2022"
vDate(1) = "12.04.2022"
vDate(2) = "14.02.2022"
Call vba_sort_array_a_to_z(vDate)
Stop
End Sub
I assume the sort algorithm is taking the first indicators into account, which would be the day of the date in this case. So "12", "14" and "25".
The only idea I had is to manipulate the whole array containing the dates by creating a temp array (as long) and insert the date values in the format YYYYMMDD to the temp array, then sorting that array and transferring the values back to another array and reformat it again in the desired target date format. This seems inefficient.
Result of the sorted temp array would look like this:

Is there another way to achieve this?
If this is the only solution, I am thinking of creating a new function out of it, which handles only the sorting of dates.


UCase...