Compare Array of strings to return the latest date then find the index with in the array

I have a array of strings {“2022-02-16”, “2022-04-15”, “2022-03-13”} i want to be able to compare these dates and return the latest date, in this case it would be “2022-04-15” then i want to be able to identify the index of this result within the array so in this case the index would be 1.

what would be the best way to do this

@duaine.b

To get the highest use the below

MaxDate = arr.Max(function(x) Cdate(x))

Or

MaxDate = arr.Max(function(x) DateTime.ParseExact(x,"yyyy-MM-dd",System.Globalization.CultureInfo.InvariantCulture))

to get the index use

Array.IndexOf(arr,MaxDate)

Hope this helps

cheers

Hi @duaine.b ,

An Alternate :

LatestDate = {"2022-02-16", "2022-04-15", "2022-03-13"}.OrderBy(Function(x)CDate(x)).Last

Here, LatestDate is of type String. Assuming you would need it as the String type itself.

Then to get the Index, same as @Anil_G suggested :

Array.IndexOf({"2022-02-16", "2022-04-15", "2022-03-13"},LatestDate)

Let us know if you are facing any issues.

1 Like

Or you can convert it to list sort and reverse. Then the first element of that list would be date you are looking for. After that index of that element in array would be the one that you are looking for.

This solution will work if the format of the date will be the same as you showed in the example.

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.