November 30, 2024

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.

Note

You don’t have to build this Power Automate yourself. Just head over to my template section and download it. If you’re not sure how to import it I have a tutorial ready right here.

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.

The call

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:

[2,4,3,1,6,8,9,10,1,22,214,55,3,2,1]

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.

The variables

As you can see, we’re defining five variables.

Let’s define each one:

  1. Array to sort – Here, we’re saving the value that we receive into an array. It will make sense further along.
  2. Sorted Array – For now it’s empty, but in the end, it will contain the values sorted
  3. 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.
  4. Min Value – The variable that will always contain the min value found.
  5. 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 process

The whole process is as follows:

  1. Find the min value (to start on the array to sort and then on the temp array)
  2. Add it to the sorted array
  3. Empty the temp array
  4. Add all other items to the temp array
  5. Repeat

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:

empty(variables('TEMP_ARRAY'))

To get the min value is also simple:

min(variables('ARRAY_TO_SORT'))

Or

min(variables('TEMP_ARRAY'))

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:

  1. Add it to the sorted array if it’s the same value as the min value
  2. 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.

Let’s return what we have:

Finally, let’s return what we have.

Looks confusing?

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.

Final thoughts

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.

Have a suggestion of your own or disagree with something I said? Leave a comment or interact on Twitter and be sure to check out other tutorial-related articles here.

Photo by Héctor J. Rivas on Unsplash

Manuel Gomes

I have 18 years of experience in automation, project management, and development. In addition to that, I have been writing for this website for over 3 years now, providing readers with valuable insights and information. I hope my expertise allows me to create compelling, informative content that resonates with the audience.

View all posts by Manuel Gomes →

5 thoughts on “Power Automate: Sort an Array

  1. Hi Manuel,

    Excellent articaul!!. This is exactly what I was looking for.

    Can you provide how the Response step is configured as I didn’t see where you explained that step. Do I just need to pass the SortedArray variable, or is there some JSON needed in the “Response Body JSON Schema” section?

    Thanks
    Ken

  2. Thanks Manuel, that’s helpful, I learned a few tricks there. What about for alphabetical sorting, including for arrays with multiple elements? Is that possible?

  3. Can’t believe after all these years, that there is no built-in actions or expressions to automatically sort arrays. Microsoft is not investing enough in Power Automate to make it truly a tool for “everyone”.

Leave a Reply

Your email address will not be published. Required fields are marked *

Mastodon