5 Ways to Sort an Array in Power Automate

Do you want to sort an array in Power Automate?

Sorting an array is a key step in many data processing tasks. It allows for easier data consumption within the array, helping save time by quickly providing optimal results.

For example, you might want to sort a set of tasks based on the assigned user so all their tasks are listed together and easy for them to find.

Sorting can also make subsequent actions such as Do until loops more efficient.

This post is going to show you how to sort arrays in your flows.

Sort Function

Power Automate has a built-in function that allows you to sort an array of values or an array of objects based on a field in the objects.

sort ( <collection>, <sortBy>? )
  • collection is a required argument which is the array of values or objects to sort.
  • sortBy is an optional argument for the key to sort by when sorting an array of objects.

The sort function will then return the array in ascending order.

Sort an Array of Values with the Sort Function

The most basic use of the sort function is to sort an unordered array of values in ascending order.

["B","A","D","E","C"]

The above array contains scalar values that appear in non-alphabetical order.

sort(outputs('Simple_Array'))

The above expression will sort the array in the compose action named Simple Array and return the array will values in ascending order.

Sort in Descending Order with the Sort Function

Unfortunately, there is no argument to choose the sort order as either ascending or descending. It will always sort in ascending order.

If you want to sort arrays in descending order you will need to use the reverse function along with the sort function.

You can first use the sort function to return an ascending order then use the reverse function to reverse the order of your sorted array which will be in descending order.

reverse ( sort ( <collection>, <sortBy>? ) )

The above expression will sort the collection in descending order.

reverse(sort(outputs('Simple_Array')))

For example, the above expression will sort the simple array from the compose action in descending order.

Sort an Array of Objects with the Sort Function

A lot of the time you will be dealing with more complex data such as arrays of records returned from external sources such as a SharePoint list or dataverse.

These are arrays of objects where each array item is a record with key-value pairs.

You can use the sort function to sort the array based on the key in a key-value pair.

[
	{"age": 32,"first": "Kayla","last": "Durram"},
	{"age": 32,"first": "Dave","last": "Smith"},
	{"age": 37,"first": "Pam","last": "LeBron"},
	{"age": 32,"first": "Zane","last": "Lyons"},
	{"age": 37,"first": "Allie","last": "Ranklin"}
]

For example, the above array contains 5 records and each record contains a person’s age, first, and last name.

You could use the sort function to return these records in ascending order based on the person’s age.

sort(outputs('Array_of_Objects'),'age')

The above expression will sort the array based on the age field in each column.

Sort an Array of Objects by Multiple Fields

The sort function only allows you to sort based on one field in a key-value pair.

But you can use nested sort functions to achieve an array sorted based on multiple columns.

sort(sort(outputs('Array_of_Objects'),'first'),'age')

For example, the above expression would sort the array by first name and then by age.

[
	{"age": 32,"first": "Dave","last": "Smith"},
	{"age": 32,"first": "Kayla","last": "Durram"},
	{"age": 32,"first": "Zane","last": "Lyons"},
	{"age": 37,"first": "Allie","last": "Ranklin"},
	{"age": 37,"first": "Pam","last": "LeBron"}
]

This would result in the above array being sorted by age and within each age the first names also are in ascending order.

Sort Arrays with ODATA OrderBy Query

Another option is to get your data sorted at the source.

Many actions include ODATA OrderBy options to import data that is sorted at the source.

For example, the SharePoint Get items action allows you to use the OrderBy option to return sorted data. The nice thing is, this can be used to sort based on multiple fields.

Age asc, FirstName desc

The above OrderBy query will sort the Age field in ascending order asc then sort the FirstName field in descending order desc.

No need to use the sort function!

Conclusions

Getting your data in order can be a vital part of your automated workflow.

Thankfully, Power Automate has a sort function that allows you to sort any array. This will also allow you to sort any array of objects based on the key in a key-value pair.

Many actions to get data from external sources also include an ODATA OrderBy option that allows you to sort your data before it’s pulled into your flow.

Have you needed to sort your data in your flows? How did you get this done? Let me know in the comments!

About the Author

John MacDougall

John MacDougall

John is a Microsoft MVP and freelance consultant and trainer specializing in Excel, Power BI, Power Automate, Power Apps and SharePoint. You can find other interesting articles from John on his blog or YouTube channel.

Related Articles

Comments

5 Comments

  1. Nicky Vlad-Christensen

    You make it sound so easy. I tried your solution, but it does absolutely nothing to my data. What could be wrong?
    I have the following stored in a variable (array of objects) named overviewEmail of type Array
    [
    {
    "numOld": "12",
    "displayName": "Christian"
    },
    {
    "numOld": "3",
    "displayName": "Anders"
    },
    {
    "numOld": "3",
    "displayName": "Steen"
    }
    ]

    I use the compose and sort
    sort(variables(‘overviewEmail’),’numOld’)
    Was this not supposed to be sorted? Output is equal to Input

    • John MacDougall

      It might be that your numbers are strings, so sorting my numOld won’t sort numerically like you’re expecting.

  2. Preston

    I tried this process and it didn’t work for me either on sorting multiple columns. This was my formula:

    sort(sort(outputs('Union_group_and_individual_clinics'),'TimeSorter'),'OverbookNumber')

    Both TimeSorter and OverbookNumber are Int values. This was my array:

    [
    {
    "TimeSorter": 33,
    "Overbook": "No"
    },
    {
    "TimeSorter": 37,
    "Overbook": "No"
    },
    {
    "TimeSorter": 37,
    "Overbook": "No"
    },
    {
    "TimeSorter": 37,
    "Overbook": "No"
    },
    {
    "TimeSorter": 33,
    "Overbook": "Yes"
    },
    {
    "TimeSorter": 35,
    "Overbook": "Yes"
    }
    ]

    It should have been:

    33 No
    33 Yes
    35 Yes
    37 No
    37 No
    37 No

    Any thoughts?

    • John MacDougall

      What is the actual resulting order?

      • John MacDougall

        It might be you are actually wanting sort(sort(outputs('Union_group_and_individual_clinics'),'OverbookNumber'),'TimeSorter')

Get the Latest Tech Tips

Write For Us

Are you a tech enthusiast with a talent for writing great content? Come write for us!

Follow Us

Follow us on social media to stay up to date with the latest in tech!