There is no built in function in Power Automate to enable you to sort an array easily. But as with my previous post in which I demonstrated how to sum an array of numbers, this can be achieved very easily with an Office Script.
There is quite a bit of demand for the ability to sort an array in Power Automate, it has received a good number of votes on the ideas forum.
Here is the array I am going to work with, which is a list of Power Automate Legends, in no particular order:
[ "Paul Murana", "Damien Bird", "Tiago Mendonça", "Leon Tribe", "Ryan Maclean", "Fausto Capellan", "Jon Levesque", "Matt Beard", "Alison Mulligan", "Antti Pajunen", "Mark Stokes", "Azure McFarlane", "Sancho Harker", "Tomasz Poszytek", "Jon Russell" ]
This flow is going to make use of two Office Scripts:
- One which sorts an array alphabetically.
- Another which reverses the order of the array.
The Script Code
Below is the code which you can insert into your Office Scripts for each of the functions. Once you have added these functions to your Office Script repository, you’re good to build the very simple flow.
function main( workbook: ExcelScript.Workbook, sortArray: Array<string> = [], ) { sortArray.sort(); return sortArray; }
function main( workbook: ExcelScript.Workbook, reverseArray: Array<string> = [], ) { reverseArray.reverse(); return reverseArray; }
Sort Arrays with Power Automate
Here is a screenshot of the very simple flow which shows you how this all comes together:

The first script action will sort the array in ascending order, and the second will reverse the order. Here are the outputs of the flow:
[ "Alison Mulligan", "Antti Pajunen", "Azure McFarlane", "Damien Bird", "Fausto Capellan", "Jon Levesque", "Jon Russell", "Leon Tribe", "Mark Stokes", "Matt Beard", "Paul Murana", "Ryan Maclean", "Sancho Harker", "Tiago Mendonça", "Tomasz Poszytek" ]
[ "Tomasz Poszytek", "Tiago Mendonça", "Sancho Harker", "Ryan Maclean", "Paul Murana", "Matt Beard", "Mark Stokes", "Leon Tribe", "Jon Russell", "Jon Levesque", "Fausto Capellan", "Damien Bird", "Azure McFarlane", "Antti Pajunen", "Alison Mulligan" ]
Script Files
You can download the scripts I created directly from here, simply stoe them in your in Onedrive \Documents\Office Scripts folder and they will appear in your scripts function in Excel connector of Power Automate.
array_sort.osts
array_reverse.osts
Performance
I tested these functions with an array of 20,000 items and it finished each action in around 5 seconds. So compared to creating a loop and sorting within Power Automate, this is blindingly quick.
Conclusion
I’m pretty excited about what is possible with Office scripts to extend the functionality of Power Automate .
Let me know how you get on, and if you think of any other useful ideas for Office Scripts we can use in Power Automate!
If you are new to Office Scripts (as I am) check out the overview from Micrsoft.