Sort an array or collection in Microsoft Flow

Have you ever tried to sort an array or collection in Microsoft Flow? Did you ever look for the Sort function when you build your expressions? You can stop searching! It doesn’t exist.

No sort function

The Sort Azure Function

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

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:

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

 

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 run

 

 

 

 

 

 

Advertisements

3 thoughts on “Sort an array or collection in Microsoft Flow

  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

    Like

Please leave a comment or feedback

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

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