sort function. Collection.(Power Automate Cloud flows)

Japanese version.

Function to sort array contents in ascending order.

If you want to sort in descending order, use the reverse function together.

Spec

sort(<collection>,<sortBy>)
ArgumentDefaultDescription
collectionRequired fieldTarget arrray.
sortByRequired for arrays with multiple columns.
Not required for single column.
For arrays with multiple columns,
Specify the column on which to base the sorting.

How to use

In many cases, it is used to initialize or set variables of type "Array".

Clicking on sort in the "Expression" tab sets the function name and () in the formula field.

If you see something like the following, your settings are complete.

You can change the contents of the function by clicking on the purple icon.

When dynamic content or variables are used in expressions, special specifications are required.

(How to get dynamic content with expressions.)

Examples of arguments and results

An array with a single column.

For example, prepare the following array.

The variable name is "srcArray".

[5,2,1,3,4]

The following expression will result in an array sorted in ascending order.

sort( variables('srcArray') )
Before sort.
After sort.

Array with multiple columns.

If there are multiple columns, you need to specify the reference column for sorting by "sortBy".

As an example, the following JSON is used to create an array.

[
	{ "Number": 1, "Name": "Z", "Qty": 500 },
	{ "Number": 2, "Name": "Y", "Qty": 300 },
	{ "Number": 3, "Name": "Z", "Qty": 400 }
]

The array will have this structure.

NumberNameQty
1Z500
2Y300
3X400

The "sortBy" field specifies the column name.

In this example, "Number," "Name," and "Qty" are the column names.

sort( variables('srcArray') ,'Qty')

When the expression is executed, it is sorted based on "Qty".

[
  {"Number": 2,"Name": "Y","Qty": 300},
  {"Number": 3,"Name": "Z","Qty": 400},
  {"Number": 1,"Name": "Z","Qty": 500}
]
NumberNameQty
2Y300
3X400
1Z500

Sort in descending order.

When combined with the reverse function, it will sort in descending order.

For example, prepare the following array.

The variable name is "srcArray".

[5,2,1,3,4]

The following expression will result in an array sorted in descending order.

reverse( sort(variables('srcArray')) )
Before sort.
After sort.

For those who want to learn Power Automate Cloud flow effectively

The information on this site is now available in an easy-to-read e-book format.

Or Kindle Unlimited (unlimited reading).

You willl discover how to about basic operations.

By the end of this book, you will be equipped with the knowledge you need to use Power Automate Cloud flow to streamline your workflow.

Links

Collection Functions(Power Automate Cloud flows)