“I can’t believe that Power Automate is missing such a basic functionality as a possibility to sort an array, is there no easy way how to do that?”
When you work with arrays or arrays of objects, you might want to organise them. And the most basic way to organise it is sorting. Sort names alphabetically, dates earliest to latest, numbers lowest to highest… But only some of the Power Automate action give you the possibility to sort the result, with the others you needed a complex workaround. Until now.
Sort in ascending order with sort() expression
As shown by Paul in his video, a few more expressions were added to Power Automate. One of them is the sort(…) expression, and as the name suggests, it’ll sort an array. Depending on the array it can have one or two parameters.
If it’s a simple array, the only parameter is the array to sort:
Note: < … > is a placeholder, replace it with your array including < and >.
If it’s an array of object, it needs two parameters – the array to sort and the sorting property:
Note: < … > are placeholders, replace them including < and >.
Sort in descending order with reverse() expression
There’s no third parameter for the sort(…) to decide on the sorting order, it’s always ascending. To sort the array the other way around you must combine it with the reverse(…) expression.
Reverse(…) by itself doesn’t sort the array, it’ll just reverse its order. Therefore, if you want a reversed array, you must sort it first and then reverse the result.
With the new Power Automate expressions it became very easy to sort any array. Instead of cumbersome workarounds you can sort any array with just one or two expressions. Sort(…) to sort it in ascending order, or combination with reverse(…) to go descending.