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:

An array of dates
An array of dates

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.

Bad dates
Bad dates

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 our bad dates
Sort our bad dates

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

Sorted fixed date
Sorted fixed date

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

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

And our dates are sorted!


Discover more from SharePains

Subscribe to get the latest posts sent to your email.

Related Posts

Leave a Reply

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