Power Automate includes the "reverse" function to flip the order of items in an array. It's straightforward - give it an array, and it returns the items in the opposite order. A common use is pairing it with the "sort" function to flip the sorted results. Keep in mind that "reverse" doesn't sort anything - if the array isn't sorted, you'll just get the same items in the opposite order.
Usage
It follows a simple pattern.
- Collection (array)
That's it - just one parameter, no options.
Here's a basic example:
reverse(createArray(0, 1, 2, 3))
will return
[3, 2, 1, 0]
The above example is sorted, but here's an example with something unsorted:
reverse(createArray(1, 4, 2, 3))
We will get:
[3,2,4,1]
With strings
It works the same with string arrays:
reverse(createArray('Alice', 'Bob', 'Charlie'))
will return
['Charlie', 'Bob', 'Alice']
With objects
When you pass an array of objects, it reverses the order of the objects themselves - it doesn't reverse anything inside the objects:
reverse(createArray(
json('{"name":"Alice","age":30}'),
json('{"name":"Bob","age":25}'),
json('{"name":"Charlie","age":35}')
))
will return
[{"name":"Charlie","age":35},
{"name":"Bob","age":25},
{"name":"Alice","age":30}]
Getting things sorted
You can wrap the "sort" function with "reverse" to sort the flipped results:
reverse(sort(variables('MyArray')))
For arrays of objects:
reverse(sort(variables('People'), 'age'))
This sorts by the "age" property and then flips the entire result.
Here's the result
[{"name": "Charlie","age": 35},
{"name": "Alice","age": 30},
{"name": "Bob","age": 25}
]
Edge Cases
Does not work on strings directly
The "reverse" function only works on arrays. You cannot pass a string to reverse its characters. If you need to reverse a string, you'll need to use the "split" function to break it into an array of characters first, reverse that array, and then use the "join" function to combine it back:
join(reverse(split('hello everyone', ' ')), ' ')
will return
'everyone hello'
Empty arrays
Reversing an empty array returns an empty array. No errors, no surprises.
Single-item arrays
Reversing an array with one item returns the same array. Again, no issues.
It doesn't sort things
You can see this in the previous examples but I want to make things clear. If you want to sort things you need the "sort" function, otherwise you'll get only a mirror of the objects in the array.
Limitations
No parameters for customization
The "reverse" function accepts exactly one parameter - the array. There's no option to reverse only part of an array or to specify a range. If you need to reverse a subset, use the "skip" function and the "take" function to extract the portion first, then reverse it.
Arrays only
As mentioned, it doesn't work on strings directly. This is strictly a collection function.
Recommendations
Here are some things to keep in mind.
Don't use it just to get the last item
If you need the last item of an array, use the "last" function instead of reversing the array and taking the first item. It's more direct and easier to read.
Always add a comment
Adding a comment will help others understand your formula. Indicate what the function is doing and why, especially if the expression is complex.
Final Thoughts
The "reverse" function is simple and does exactly what you'd expect - it flips the order of an array. It pairs well with the "sort" function when you need to flip sorted results, a pattern you'll use often. Keep it in your toolkit for any scenario where order matters.
Sources
Microsoft's "reverse" Function Reference
Back to the Power Automate Function Reference.
Photo by Siddhesh Mangela on Unsplash
No comments yet
Be the first to share your thoughts on this article!