I see this question asked over and over in the forums. “How do I sort an array.” The usual reply is, “you can’t, “ and that annoys me. Although Power Automate doesn’t have a standard way to sort an array, it’s something I use all the time and want to show you how to do it.
Before we start
I’ll design this as an “auxiliary” Power Automates, meaning that you can use it in any other Power Automates that you create. Think of it as using your work efficiently where you have code that is shared by multiple areas, so you don’t need to do it again and again. Also, if you notice an issue, you can fix it in one place instead of multiple.
Let’s start with numbers
Numbers are usually more straightforward, and we have some functions that can help us in some of the tasks. So let’s say that you have a bunch of numbers that you want to sort. It doesn’t matter why, but that’s what you need. As noted before, Power Automate doesn’t allow for that, but here’s how to do it.
The Power Automate
First, let’s show the full picture so that you know that it’s not too complicated.
It looks simple right? So let’s break it down.
Calling this Power Automate is super easy. We’ve done it in the past, but here’s a quick refresh.
Anywhere in your Power Automate, you can call other Power Automates by using an HTTP Post. It can look quite technical, but once you get the hang of it, it’s quite simple. In this case, we just need to pass an array of numbers. An array is represented as [ ] and insert all the numbers as follows:
To do the HTTP Post, you can do the following:
As you can see, unordered array in and ordered array out.
Getting the array
Getting the array is simple. Define an HTTP trigger that is defined to get an array of ints.
Then just pass the result along to the next steps.
As you can see, we’re defining five variables.
Let’s define each one:
- Array to sort – Here, we’re saving the value that we receive into an array. It will make sense further along.
- Sorted Array – For now it’s empty, but in the end, it will contain the values sorted
- Temp Array – I’ll use this array to store the remaining values in each step of the process. For example, if I have an array that I find the number 1, I add it to the sorted array and add all the other numbers to the temp array.
- Min Value – The variable that will always contain the min value found.
- Empty Array – This variable is just a convenience more than anything. I’ll need to reset some of the arrays above, and set them to this variable will make them empty.
The whole process is as follows:
- Find the min value (to start on the array to sort and then on the temp array)
- Add it to the sorted array
- Empty the temp array
- Add all other items to the temp array
Find the min value
Why the distinction between the array with all the items and the temp array. It’s simple. We want the temp array to have all the remaining unsorted elements. Then we can just fetch the min value of the unsorted group and parse it. When it’s empty, we’re finished, and we have the array sorted.
To check if the array is empty is quite simple:
To get the min value is also simple:
Nothing to complex here.
Empty the array
A simple set variable will do the trick:
Let’s check if there are no mistakes and the min value is not yet in the sorted array
With this, we’re sure that we’re not putting values in the array that should not be there.
Find duplicates and move the other values to the temp array
In an array, you can have multiple instances of the same number, so you need to be sure to catch all of them. So we’ll check all remaining numbers and, for each:
- Add it to the sorted array if it’s the same value as the min value
- Add to the temp array if it’s not
Add the value to the temp
The number is not the same as the min number, so you can add it to the temp array for further parsing.
You may look at it and think it’s confusing, but it’s quite simple. We need a way to continually fetch the min value and remove all occurrences of it from the unsorted array and add the remaining items in the temp array. Then repeat.
Of course, if we had an excellent function or action to sort our arrays, our life would be a lot easier but, for now, we have to find these workarounds to get things rolling. You can use this while Microsoft doesn’t release a better solution and then swap it once it’s out. I hope this helps you in your Power Automates.