Earlier Date among 3 variables

I have 3 string variables in which the value could be either date in dd/MM/yyyy format or blank.
I need to find the earlier date among the values found in these 3 variables

Eg:
Scenario:1
Var1 = 12/07/2020, Var2 = 15/08/2020, Var3 = 20/05/2020 -> output = 20/05/2020
Scenario:2
Var1 = 12/12/2020, Var2 = 15/08/2020, Var3 = “” -> output = 15/08/2020
Scenario:3
Var1 = “”, Var2 = “”, Var3 = 20/05/2020 -> output = 20/05/2020

If you have any idea regarding this please let me know

Hi,

Can you try the following?

arrString = {Var1,Var2,Var3}

result = arrString.Where(Function(s) not String.IsNullOrEmpty(s)).OrderBy(Function(t) DateTime.ParseExact(t,"dd/MM/yyyy",Nothing)).First()

Regards,

2 Likes

That’s Amazing. It is working fine
Thanks for your help

1 Like

Hi,

If I need to sort desc, what changes I need to do?

We can use OrderByDescending method instead of OrderBy. Can you try?

Regards,

Thanks Again,
I found .Last() also works fine

1 Like

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