No sort function

Have you ever tried to sort an array or collection in Power Automate?

The sort function in Power Automate

Did you ever look for the Sort function when you build your expressions?

You can stop searching! It doesn’t exist. There is no easy way to sort an array. But it is not impossible to sort your array.

No sort function

To filter an array is easy, as there is an action for that, but sorting can be a real pain.

The Sort an array Azure Function

You could however create an Azure function. Only recently the option to create Powershell v2 functions Apps can be created in Azure.

Sort an array PowerShell Function

You can of course do similar thing in C# but, isn’t PowerShell a lot easier?

Once the function has been created you will get the following example:

Function Template

The above code needs to be adjusted so that my json is received and sorted and then returned.

The above code I’m replacing with:

using namespace System.Net

# Input bindings are passed in via param block.
param($Request, $TriggerMetadata)

# Write to the Azure Functions log stream.
Write-Host "PowerShell HTTP trigger function processed a request."

# Interact with query parameters or the body of the request.
$myArray = $Request.Query.MyArray
if (-not $MyArray) {
$myArray = $Request.Body.MyArray
}

if ($myArray) {

$myArrayObj = ConvertFrom-JSON $myArray

$mySortedArrayObj = $myArrayObj | Sort-Object -Property Title

$body = ConvertTo-Json $mySortedArrayObj
$status = [HttpStatusCode]::OK
}
else {
$status = [HttpStatusCode]::BadRequest
$body = "Please pass a json on the query string or in the request body."
}

# Associate values to output bindings by calling 'Push-OutputBinding'.
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
StatusCode = $status
Body = $body
})

The Flow

My sort an array Flow

The first Compose contains the following unsorted json collection:

[{Title:'Title 1', Description:'Description 1'},
{Title:'Title 3', Description:'Description 3'},
{Title:'Title 2', Description:'Description 2'},
{Title:'Title 5', Description:'Description 5'},
{Title:'Title 4', Description:'Description 4'}]

The second Compose validates my json. This isn’t really needed but always good to do during tests like this.

Then the 3rd Compose returns the result.

Sort an array flow run

By Pieter Veenstra

Business Applications and Office Apps & Services Microsoft MVP working as a Microsoft Productivity Principal Consultant at HybrIT Services. You can contact me using contact@veenstra.me.uk.

4 thoughts on “Sort an array in Power Automate”
  1. Hi Pieter,

    Me again! Though I have figured out my flow, but I have some crazy ideas, not sure if they can be accomplished in Flow.

    #1 Filter Query in ODATA query, while use key eq ‘key’, can I do something like trim(tolower()) in both sides? Otherwise I have to do this in the source data;
    #2 After this filter, it will be apply to each, can I join the results to one array? I tried Compose/Select/Set Variable…nothing worked….
    #3 Is it a crazy idea to do bulk update/create in a shared spreadsheet? – My concerns are all about the source data control, the Flow is not difficult.

    Many thanks,
    Vera

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.