While working on a project, I received a requirement to sort an array in Power Automate. Though I was new to it, I searched, implemented, and found the solution. So, I thought I would share everything in this post.
Power Automate has a collection function called the sort() function, which changes the arrangement of array elements in ascending and descending order.
In this tutorial, I will explain the 6 various ways to sort an array in Power Automate:
- Power Automate Sort an Array Alphabetically
- Power Automate Sort Array Descending
- Power Automate Sort an Array Objects by Multiple Fields
- Power Automate Sort Nested Array Value
- Power Automate Sort an Array by Date
- Power Automate Sort an Array using Get Items Order By
Power Automate Sort function
The Power Automate sort function converts the order of items in a collection to an ascending or descending manner.
It will sort the array of objects based on any field that contains a simple value.
sort ( <collection>, <sortBy>? )
Parameters:
- Collection: It is a mandatory field that contains an array of elements or objects to sort.
- sortBy: It is optional to sort the array of objects based on key values.
Input | Output |
New York, Australia, Chicago, Yemen | Australia, Chicago, New York, Yemen |
Power Automate Sort an Array Alphabetically
To sort an array alphabetically, you need to use the Power Automate sort function.
Example:
Input | Output |
New York, Australia, Chicago, Yemen | Australia, Chicago, New York, Yemen |
Follow the below-mentioned steps:
1. Open the Power Automate Home page, click on + Create, select the Instant cloud flow, provide the flow name, choose the flow trigger (Manually trigger a flow), and Click on the Create button.
2. Take an Initialize Variable flow action to create an array of elements. Configure the below details:
- Name: Enter a name for the variable.
- Type: Select variable type as ‘Array‘.
- Value: Insert the below expression here.
createArray('New York','Australia','Chicago', 'Yemen')
You can replace the elements with the elements you need.
3. Next, add a Compose flow action to sort the created array in ascending order.
Inputs: Click on Expression -> Insert the code below.
sort(variables('Countries'))
4. The flow is ready now. Click on Save. Test it Manually. Select the Runflow button.
5. The screenshot below represents the ascending order of an array’s elements in compose flow action.
This is how to sort an array within the Power Automate flow using the sort function in an ascending manner.
Power Automate Sort Array Descending
To sort an array in descending order, we have to use the reverse function along with the sort function.
As shown in the above example, follow the same steps:
1. Use an Initialize variable to create an array with the number values as shown below. Provide a Name, Type, and Value.
2. Inside the Compose action inputs section, insert the expression that is given below:
reverse(sort(variables('Salary')))
Save and run the flow.
Output:
The sorted array values will be displayed in the compose section in descending order.
This way, you can sort an array in descending order with the Power Automate sort function
Power Automate Sort an Array Objects by Multiple Fields
To sort an array with the objects by multiple fields, check out the below steps:
1. Inside the Initialize variable, take an array of Objects in the Value section, as you can see from the below image:
Provide the parameters like Name, Type, and Value.
2. In the Compose flow action, add the below-given expression in the Inputs parameter:
sort(sort(variables('User Registration'),'Country'),'UserName')
This expression results in the above-given array being sorted by UserName, and within that, the Country values are also in ascending order.
Now, save and run the flow manually.
Output:
To check the result, open the compose action and see the outputs.
Here, It will sort the array of objects according to the given key value, as shown in the below screenshot:
This way, we can sort an array of objects by using the Power Automate sort function.
Power Automate Sort Nested Array Value
To sort the nested arrays using Power Automate, check out the below steps:
1. Select a Power Automate Instant cloud flow.
2. Take a Compose flow action with the nested array in the Inputs parameter, as shown below.
{
"statusCode": 200,
"headers": {
"Content-Length": "45276"
},
"body": {
"value": [
{
"Name": "Robert",
"id": "TS325",
"properties": {
"startDate": "2023-02-01",
"status": "Succeeded"
}
},
{
"Name": "Warner",
"id": "TS896",
"properties": {
"startDate": "2023-09-01",
"status": "Running"
}
},
{
"Name": "Alex",
"id": "TS741",
"properties": {
"startDate": "2024-02-01",
"status": "Succeeded"
}
},
{
"Name": "Joseph",
"id": "TS561",
"properties": {
"startDate": "2024-06-01",
"status": "Succeeded"
}
}
]
}
}
Condition | Result |
Sort based on Start Date, which has Status as Succeeded | Start Date value in ascending order |
3. Now, add a Filter array flow action to filter out the array objects where the status is equal to succeed.
Configure the parameters:
- From: Provide @{outputs(‘Compose’)?[‘body/value’]}.
- Filter Query: Insert the below query in it.
@equals(@{item()?['properties/status']},'Succeeded')
4. Next to that, add a Select action and provide the below details:
To sort the StartDate value, we have to make use of the addProperty() function, which will take the value to the top level of each object.
- From: Select the Body of the filter array from the dynamic content.
- Map: Add the below-given expression placed in the code below.
addProperty(item(), 'startDate', item()?['properties/startDate'])
5. After that, take a Compose flow action to provide the sort() expression.
sort(body('Select'),'startDate')
6. Save and run the flow manually to test it.
Output:
You can see from the below picture that the values of startDate have been sorted in ascending order.
This is all about how to sort nested arrays using Power Automate.
Power Automate Sort an Array by Date
To work with sorting an array by date in Power Automate, follow the below example.
For this, I will retrieve the values from a SharePoint list with a couple of columns, as shown in the image below:
I wanted to sort the Task Deadline column values in ascending order.
Below are the steps to do this example:
1. Create an Instant cloud flow in Power Automate.
2. Then, add the ‘Get Items‘ flow action to retrieve all the SharePoint list items. Fill in the required details:
- Site Address: Provide a SharePoint site.
- List Name: Choose a SharePoint list from the drop-down.
- Limit Columns by View: Select the list view that you have created.
3. After that, add a Compose flow action to sort the list column values by date. Provide the below expressions in the Inputs section.
sort(outputs('Get_items')?['body/value'],'TaskDeadline')
4. Next, add the Create HTML table flow action. In the From section, add the output of the compose flow action.
5. Finally, add a Send an email(V2) flow action and provide the required details like To, Subject, and Body.
6. Once the Power Automate flow is ready, save and run the flow manually.
7. If there is no error, the flow runs successfully. You will receive an Outlook email.
It will show the sorted order of an array element [By Dates].
This is all about sorting an array by dates with Power Automate expression.
Power Automate Sort an Array using Get Items Order By
We can also use the Get items flow action in Power Automate to sort the data from the SharePoint list according to the list columns in ascending or descending order.
I have written a complete tutorial on Power Automate Get Items Order By. Check out the tutorial by clicking the above link.
Conclusion
I trust this tutorial gave you a clear view of how to sort an array in ascending/descending order, sorting multiple fields of an array, sorting nested array values and sorting an array by date in Power Automate.
Also, you may like:
- Delete Files Older than 3 Years from SharePoint Document Library Using Power Automate
- Post an Adaptive Card to Microsoft Teams Using Power Automate
- Update SharePoint list items using Power Automate
- Set Microsoft Teams Status Using Power Automate
- Power Automate Approval Reminder
After working for more than 15 years in Microsoft technologies like SharePoint, Office 365, and Power Platform (Power Apps, Power Automate, and Power BI), I thought will share my SharePoint expertise knowledge with the world. Our audiences are from the United States, Canada, the United Kingdom, Australia, New Zealand, etc. For my expertise knowledge and SharePoint tutorials, Microsoft has been awarded a Microsoft SharePoint MVP (9 times). I have also worked in companies like HP, TCS, KPIT, etc.