Power Automate: sort Function

Power Automate: sort Function

by: Manuel ⏱️ 📖 4 min read 💬 0

Power Automate includes the "sort" function to arrange items in an array in ascending order. It can handle simple arrays of strings or numbers, and it also supports sorting arrays of objects by a specific property. If you need descending order, pair it with the "reverse" function.

Usage

It follows a simple pattern.

  1. Collection (array)
  2. (Optional) sortBy -- the property name to sort by, when sorting arrays of objects

Simple arrays

For arrays of numbers or strings, just pass the array:

sort(createArray(3, 1, 4, 1, 5, 9, 2))

will return

[1, 1, 2, 3, 4, 5, 9]

For strings, it sorts alphabetically:

sort(createArray('Charlie', 'Alice', 'Bob'))

will return

['Alice', 'Bob', 'Charlie']

Arrays of objects

When sorting objects, provide the property name as the second parameter. Usually the array comes from a variable or another action, but here we're using the "createArray" function to make the example easier to follow:

sort(createArray(json('{"name": "Charlie", "age": 35}'), json('{"name": "Alice", "age": 30}'), json('{"name": "Bob", "age": 25}')), 'name')

will return

[
  {"name": "Alice", "age": 30},
  {"name": "Bob", "age": 25},
  {"name": "Charlie", "age": 35}
]

Descending order

There's no descending parameter. Wrap the "sort" function with the "reverse" function:

reverse(sort(createArray(3, 1, 4, 1, 5, 9, 2)))

will return

[9, 5, 4, 3, 2, 1, 1]

For objects:

reverse(sort(createArray(json('{"name": "Charlie", "age": 35}'), json('{"name": "Alice", "age": 30}'), json('{"name": "Bob", "age": 25}')), 'age'))

Edge Cases

Numbers stored as strings

This is the most common gotcha with the "sort" function. If your numeric values are stored as strings, they get sorted alphabetically instead of numerically.

⚠️String numbers sort incorrectly

An array like ["1", "10", "100", "30", "4"] sorts to ["1", "10", "100", "30", "4"] instead of the expected ["1", "4", "10", "30", "100"]. This happens because string comparison works character by character -- "10" comes before "4" because "1" comes before "4."

The workaround: Use a Select action to add a numeric property using the "int" function, then sort by that new property. If your array looks like this:

[
  {"number": "30"},
  {"number": "4"},
  {"number": "10"}
]

Use this expression in the Select action to add a numeric version of the property:

addProperty(item(), 'numberSort', int(item()?['number']))

The "addProperty" function takes each object and adds a new property called "numberSort." The value comes from the "int" function, which converts the string in item()?['number'] to an actual number.

The Select action will output:

[
  {"number": "30", "numberSort": 30},
  {"number": "4", "numberSort": 4},
  {"number": "10", "numberSort": 10}
]

Then sort by the converted property:

sort(body('Select'), 'numberSort')

Now the result will be correctly sorted by numeric value: 4, 10, 30.

Date sorting

Dates can have the same problem as numbers stored as strings. If your dates are in a format like "MM/DD/YYYY," they'll sort alphabetically by month rather than chronologically. Use ISO 8601 format ("yyyy-MM-ddTHH:mm:ssZ") for reliable date sorting, or convert them using the "formatDateTime" function before sorting.

Case-sensitive string sorting

String sorting is case-sensitive. Uppercase letters sort before lowercase letters:

sort(createArray('alice', 'Bob', 'Charlie'))

will return

['Bob', 'Charlie', 'alice']

"Bob" comes before "alice" because uppercase "B" has a lower character code than lowercase "a." If you need case-insensitive sorting, use the "toLower" function or the "toUpper" function to convert all strings to the same case before sorting.

Limitations

Ascending only

The "sort" function always sorts in ascending order. There's no third parameter for sort direction. Use reverse(sort(...)) for descending.

Single property only

You can only sort by one property at a time. If you need to sort by multiple properties (like sorting by last name, then by first name), you'll need to nest sort calls:

sort(sort(variables('People'), 'firstName'), 'lastName')

The inner sort runs first, then the outer sort reorders by the second property. Items with the same outer sort value retain their inner sort order.

No custom comparator

You can't provide a custom comparison function. The sort is always based on the natural order of the values (alphabetical for strings, numerical for numbers).

Recommendations

Here are some things to keep in mind.

Convert data types before sorting

If you're sorting numbers, make sure they're actually numbers, not strings. If you're sorting dates, make sure they're in ISO 8601 format. Data type mismatches are the number one cause of unexpected sort results.

Use reverse for descending

The reverse(sort(...)) pattern is the standard approach in Power Automate. It's clean and well-understood by anyone reading your Flow.

Test with real data

Sorting behavior can surprise you, especially with mixed data types, special characters, or locale-specific strings. Test your sort expression with a representative sample of your actual data before deploying.

Always add a comment

Adding a comment will help others understand your formula. Indicate what the function is doing and why, especially if the expression is complex.

Final Thoughts

The "sort" function is essential for organizing data in Power Automate. It works great out of the box for simple arrays, but watch out for the string-vs-number gotcha when sorting objects. Pair it with "reverse" for descending order, and always make sure your data types are correct before sorting. Those two habits will save you from the most common issues.

Sources

Microsoft's "sort" Function Reference

Back to the Power Automate Function Reference

Photo by lan deng on Unsplash

Comments

💬

No comments yet

Be the first to share your thoughts on this article!

Leave a Comment

All comments are reviewed for spam before being displayed 5000 left
Replying to