Until recently it was not possible to easily sort arrays or collections of objects in Power Automate using a single expression, either complex logic techniques or Office Scripts were required.
But recently two new handy expressions became available in Power Automate that make it really simple to do:
- sort
- reverse
How the sort expression work
The sort expression accepts either an array with simple values or a collection of objects.
If you pass a simple array to the expression, no parameter is needed. For example, if you have the following array variable with people’s names:
[
"Michel",
"Nick",
"Michael",
"Carlos",
"Renan"
]
By using the sort expression as below:
sort(variables('Names'))
You will get as output:
[
"Carlos",
"Michael",
"Michel",
"Nick",
"Renan"
]
If you have an array/collection with objects, you need to specify a property to use in the sorting, so for example, consider an array variable named ‘NamesCountries’ as below:
[
{
"firstName": "Donovan",
"lastName": "Kristovich",
"country": "Georgia"
},
{
"firstName": "Kory",
"lastName": "Melot",
"country": "Azerbaijan"
},
{
"firstName": "Gabriel",
"lastName": "Balenzuela",
"country": "Hong Kong"
},
{
"firstName": "Reed",
"lastName": "Dunne",
"country": "Cape Verde"
},
{
"firstName": "Sylvester",
"lastName": "Mavros",
"country": "Sudan"
}
]
The output of the below formula will sort the arrays by Country:
sort(variables('NamesCountries'),'country')
Note the country order: “Azerbaijan”, “Cape Verde”, “Georgia”, “Hong Kong”,”Sudan”
[
{
"firstName": "Kory",
"lastName": "Melot",
"country": "Azerbaijan"
},
{
"firstName": "Reed",
"lastName": "Dunne",
"country": "Cape Verde"
},
{
"firstName": "Donovan",
"lastName": "Kristovich",
"country": "Georgia"
},
{
"firstName": "Gabriel",
"lastName": "Balenzuela",
"country": "Hong Kong"
},
{
"firstName": "Sylvester",
"lastName": "Mavros",
"country": "Sudan"
}
]
How the reverse function works
The reverse function will only revert an array order, regardless of whether it is sorted or not. So for example, if we run it against the initial array of names mentioned above as below:
reverse(variables('Names'))
The output will be a reversed order array from the original one:
[
"Renan",
"Carlos",
"Michael",
"Nick",
"Michel"
]
There is no second parameter accepted in this expression. If we use it against an array of objects, it will simply reverse the order of it. For example, using the below expression on top of the array of objects mentioned above:
reverse(variables('NamesCountries'))
Gives the output a reversed array based on its initial order:
[
{
"firstName": "Sylvester",
"lastName": "Mavros",
"country": "Sudan"
},
{
"firstName": "Reed",
"lastName": "Dunne",
"country": "Cape Verde"
},
{
"firstName": "Gabriel",
"lastName": "Balenzuela",
"country": "Hong Kong"
},
{
"firstName": "Kory",
"lastName": "Melot",
"country": "Azerbaijan"
},
{
"firstName": "Donovan",
"lastName": "Kristovich",
"country": "Georgia"
}
]
In case we need to sort items from a collection of objects/array in descending order, we can use a combination of both sort and reverse formulas.
For example, the below formula using the names/countries array:
reverse(sort(variables('NamesCountries'),'country'))
Will have as an output an array with items sorted by Country in descending order (“Sudan”,”Hong Kong”,”Georgia”,”Cape Verde”,”Azerbaijan”):
[
{
"firstName": "Sylvester",
"lastName": "Mavros",
"country": "Sudan"
},
{
"firstName": "Gabriel",
"lastName": "Balenzuela",
"country": "Hong Kong"
},
{
"firstName": "Donovan",
"lastName": "Kristovich",
"country": "Georgia"
},
{
"firstName": "Reed",
"lastName": "Dunne",
"country": "Cape Verde"
},
{
"firstName": "Kory",
"lastName": "Melot",
"country": "Azerbaijan"
}
]
Conclusion
By using the new sort and reverse expressions we can easily sort arrays and collections of objects, and avoid using complex techniques as it was needed earlier.
[…] Source link […]
Thank you for this! I have one question: I am attempting to use this expression in a compose box to sort by a numeric ascending value, but it is sorting that value as if it were a word and not a number. How would I amend this expression to sort the array by a number value that is apparently stored as a text string?
Hello Scott,
I have made the response in this blog post:
https://michelcarlo.com/2023/08/07/sort-an-object-array-using-number-values-that-are-stored-as-string-in-power-automate/