When we use the Sort function to sort arrays in Power Automate, it is soo much easier than the 3 step approach that we had before. But when you try to sort an array by dates, there are a few potential issues when you really start to use this function.
Sort an array by Dates
When we sort dates in Power Automate we really want to to work with dates like this:
We have some dates generated by for example a get items action in SharePoint. Or anything else that generates the dates and all dates are nicely formatted optimized for automation processes.
But what if we have a bad date format returned by an API? Yesterday I was contacted on the chat about an API that didn’t return these nice to work with dates.
In the example below you can see the issue.
As you can see the dates are sorted by month, then day and then year as the dates are sorted alphabetically. This is of course no use at all.
How can we deal with that?
Obviously just using the sort date in this case will not be good enough.
Fixing the dates
To fix the dates, all we have to do is use a select action that converts the date format often used in the US to the sortable date format. The expression used for this is:
formatDateTime( item(), ‘yyyy-MM-dd’)
And for the sorting we now use the following one liner:
And our dates are sorted!