Many people struggling with the Sort Function in Microsoft. You should be surprised to know that the said function can be used to Sort Arrays in Power Automate. The new approach is far easier than the traditional Three Step Approach. However, there are a few things you should know about before sorting array by dates.
Using Sort Function to sort Dates:
The first approach is to sort dates in Power Automate like the example below:
STEP 1
STEP 2
Here you can see that some dates have been generated for example get items in SharePoint. Moreover, all dates are nicely formatted for the sake of automation processes.
Caution
If we have a bad date format it can be problematic as it can be returned by API. See the example below:
STEP 1
STEP 2
In the examples above you can see that dates have been sorted by month rather than day. This is not of any use to anyone. If such cases appear, you shouldn’t be worried because there are easy ways which can be used to fix them.
Fixing the Date:
To fix this problem all you need to do it to Select Action which will allow you to convert the Date Format in the US. The expressions below can be used to achieve it:
formatDateTime( item(), ‘yyyy-MM-dd’)
For sorting you can use this one-liner command:
sort(body(‘Select’),’Date’)
Voila! Everything is sorted out.
Final Word
It is time to bid farewell to the old Three Step Approach and welcome the new approach which is way easier to deploy and easy to learn the best thing is it does not matter how many times you may need to use it because it is done in a few minutes.