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.
Azure McFarlane says
I feel so honored to make it on your blog! Keep up the great work Paulie 🙂
Paulie says
Thank you Azure – pleasure to have you here!
Matthew Devaney says
Paulie,
Did you use the compose step to build the initial array? I ask because I cannot find any action called “Array”
Paulie says
Hi Matthew, Sorry – I did not that make very clear. Yes, it is just a compose action with a manually entered JSON array. But an array from any action will also work fine.
Paulie says
Matthew – May I also ask, what is the source of your array? I discovered another way to do it yesterday which I plan to do a video on which is a bit more native to the platform.
Hendra says
Hi Paul,
I tries the sorting, but the result came back not sorted, still the same order. Any ideas why this happened ?
Mike Greenway says
Hi, this is brilliant, thanks so much. I am perhaps running before learning to walk, but have used this with an array (a ‘List Folder’ action) to alphabetise the list. The returned result is an array of single strings, each record is one string, comma separated. I need to then do further work as if it was the original array, but am struggling to convert it back. Can you give me some tips please? Thanks, Mike
Jonathan Cutting says
Hi, Paulie!
It seems Microsoft changed something in the Run Script control in Power Automate, and your solution no longer works. The Run Script control passes the array as a string literal, so the function sees it as a one-element array. So, an array like [“b”, “c”, “a”] is passed as “[\”b\”, \”c\”, \”a\”]”, and the function returns [“[\”b\”, \”c\”, \”a\”]”].
The solution I found is to pass the array to the script as a string, then add a couple extra lines to the script to split the string into an array using the comma delimiter. Here’s the code I used that worked…
function main(workbook: ExcelScript.Workbook, strSort: string) {
var sortArray: Array = strSort.split(“,”);
sortArray.sort();
return sortArray;
}
Now, when I pass the function the string “b,c,a”, it returns the array [“a”, “b”, “c”].