6 Various Ways to Sort Array in Power Automate

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.
InputOutput
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:

InputOutput
New York, Australia, Chicago, YemenAustralia, 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.

Power Automate sort an array

3. Next, add a Compose flow action to sort the created array in ascending order.

See also  How to Move files from one folder to another using Power Automate?

Inputs: Click on Expression -> Insert the code below.

sort(variables('Countries'))
How to sort an array using Power Automate

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.

How to sort an array with Power Automate

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.

Power Automate sort array descending

2. Inside the Compose action inputs section, insert the expression that is given below:

reverse(sort(variables('Salary')))
Sort an arry of elements in descending using Power Automate

Save and run the flow.

Output:

The sorted array values will be displayed in the compose section in descending order.

How to order an array in descending using Power Automate

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.

Power Automate sort an array with multiple fields

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.

How to sort an array with multiple fields using Power Automate

Now, save and run the flow manually.

Output:

To check the result, open the compose action and see the outputs.

See also  Power Automate Skip Function

Here, It will sort the array of objects according to the given key value, as shown in the below screenshot:

Sort an Array of Objects by Multiple Fields using Power Automate

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"
        }
      }
    ]
  }
}
Power Automate Sort By Nested Array Value
ConditionResult
Sort based on Start Date, which has Status as SucceededStart 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')
Sort By Nested Array Value using Power Automate

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'])
How to sort nested arrays using Power Automate

5. After that, take a Compose flow action to provide the sort() expression.

sort(body('Select'),'startDate')
Sort by nested json array in Power Automate

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.

Sort nested array values using Power Automate flow

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:

See also  PowerApps stuck on getting your data

I wanted to sort the Task Deadline column values in ascending order.

Power Automate sort array by date

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.
Sort an array by dates in Power Automate

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')
How to sort an array values by date in Power Automate

4. Next, add the Create HTML table flow action. In the From section, add the output of the compose flow action.

How to use Power Automate to sort an array

5. Finally, add a Send an email(V2) flow action and provide the required details like To, Subject, and Body.

How to sort array with Power Automate

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].

Sort an array of elements using Power Automate

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:

>