← Al Davies, 250 High Street

How to sort an array in Power Automate

Use the sort() expression. For a plain list, write sort(variables('Names')). For a list of objects, add the key name as the second argument, such as sort(body('Select'), 'Title').

What does the sort() expression do?

The sort() function puts the items of a collection in order. Microsoft documents it as sort([collection], sortBy?). The collection is required. The sortBy key is optional.

Microsoft shows this example: sort(createArray(2, 1, 0, 3)) returns [0,1,2,3]. For objects, you can sort by any key that holds a simple value, such as an integer, decimal, string, or true/false value.

How do you sort a list of text or number values?

  1. Place a Compose action below the step that builds your array.
  2. Click the input box and open its Expression tab.
  3. Type sort(outputs('Array')), with the name of your own action in place of Array.
  4. Select Add, then save and run the flow.
  5. Check the run history. The output lists the values in order.

How do you sort a list of objects by one column?

Pass the key name as the second argument. For example, sort(body('Select'), 'Title') sorts the rows by the Title key. Write the key name in single quotes and match the spelling exactly.

Use a Select action first when your data comes from SharePoint. A Get items action returns many nested fields. Select lets you build a flat object with simple keys, which are easier to sort.

How do you sort in descending order?

Wrap the result in reverse(). Microsoft states that reverse() used with sort() sorts a collection in descending order.

Write reverse(sort(body('Select'), 'Amount')). Put the key inside sort(). The reverse() function takes only the collection.

Can you sort by more than one column?

The sort() function takes one key. The Microsoft reference lists no second sort key. To sort by two columns, build one combined key in a Select action, such as a text value with the first column, a dash, and the second column. Then sort by that key.

If you only need the order inside a SharePoint view, see sort a SharePoint list by multiple columns. To sort at the source, use the Order By field in the Get items action.

Why do numbers or dates come out in the wrong order?

A value stored as text sorts as text. The text 10 can land before the text 9. Convert the value first. In a Select action, map int(item()?['Quantity']) or float(item()?['Amount']) instead of the raw value.

Test with your own data and check the order in run history. After sorting, use a Filter array action to keep only some rows, or a Join action to build one text line from the sorted list.

FAQ

Is there a Sort action in Power Automate?

No. Use the sort() expression in a Compose, Set variable or Select input.

What happens if I leave out the key on an array of objects?

The key is optional in the reference, but objects need a key to sort by. Add the key name as the second argument.

Does sort() change the original array?

No. It returns a new sorted array. Use that output in later steps.

Sources