Sorting Array of Objects in Logic Apps or Power Automate
Introduction
This article will show how to sort an array of objects in Logic Apps (or Power Automate) in ascending or descending order. For simplicity’s sake, I will refer to using Logic Apps throughout the article, but the process will apply to either.
Body
Let’s start with assuming we have some process in our Logic App that results in us having a JSON array of objects. Take this sample JSON data…
…and it would look something like in a Logic App output:
As you can see, the first record in the array has a Date of 2022–10–25. If I wanted this array to be in descending order, I would have to manipulate it in some way. Here is how you can apply sorting to this array.
Step 1
Add a new Compose action underneath the action with the array output
Since our example output is from the Select action, let’s add a new activity underneath to run after the Select action completes. Hit the + sign underneath the activity to add a new step.
Search for the Compose action, under the category of Data Operations, and select it to add to the Logic App canvas. Once it’s been added, click into the inputs box, and select Add dynamic content.
Step 2
Add the sort function to the expression.
Select the Expression tab, and type in sort() to start building the function call.
Step 3
Add the array output as the first argument.
Click inside the parentheses for the sort function, swap to the Dynamic content tab, and select the Output from the previous action. This adds the output from the action as the first argument for our sort function. For our example, it was the Select action output.
Clicking the Output for Select in our Dynamic content tab added the body(‘Select’) syntax automatically for us.
Step 4
Add the second argument for the sort function, which is the name of the key in the objects that we want to sort by.
This second argument is optional. If our array was just an array of values, and not objects, we shouldn’t pass a second argument because there would be no keys to sort on. But if it’s an array of objects, and we don’t pass a second argument, the Logic App will error out because it won’t know how to sort the objects without a key name passed. Since we are sorting an array of objects in our example, then we must pass a second argument.
In our example, this will be the Date key. Add a comma after the first argument, swap back to the Expression tab, and hard-code in the name of the column we want to sort by. This column name should be wrapped in single quotes, not double.
If wanting to sort in ascending order, we are done!
Just click OK, and this Dynamic expression has been added to the Compose action. Now we can reference the output from our Compose action to operate on an array sorted in ascending order.
However, if needing to sort by Date in descending order, we need to do one more step.
Step 5
Add a reverse function to sort the array in descending order.
If we wanted to sort our array in descending order, we just need to insert a reverse function that accepts all the logic from Step 4 as its argument.
If wanting to sort in descending order, we are done!
Just click OK, and this Dynamic expression has been added to the Compose action. Now we can reference the output from our Compose action to operate on an array sorted in descending order.
Here is what the descending array output would like in our example:
As you can see, the Outputs from Select action was not sorted how we wanted, but after adding the sort logic to our array, the Compose action now has our array sorted in descending order.
Conclusion
Working in Logic Apps and Power Automate typically requires working with JSON data. Learning how to manipulate these arrays to our needs is a crucial step in us all becoming better developers. Hope this helps.
Thanks for reading!