Sort Dates

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:

Sort an array by dates in Power Automate Microsoft Power Automate image 1

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.

Sort an array by dates in Power Automate Microsoft Power Automate image 2

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.

Sort an array by dates in Power Automate Microsoft Power Automate image 3

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’)

Sort an array by dates in Power Automate

And for the sorting we now use the following one liner:

sort(body('Select'),'Date')

And our dates are sorted!

Avatar for Pieter Veenstra

By Pieter Veenstra

Business Applications and Office Apps & Services Microsoft MVP working as a Microsoft Productivity Principal Consultant at HybrIT Services. You can contact me using contact@veenstra.me.uk.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: